javascript求图形的面积的方法:【function circle(r){ if(this instanceof circle){this.r=r; this.area=function(){return math.pi*this】。
本文操作环境:windows10系统、javascript 1.8.5、thinkpad t480电脑。
图形面积求解:
第一种:构造函数
<!doctype html><html><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <title>document</title> <script> function retangle(a,b){ if(this instanceof retangle){ this.a=a; this.b=b; this.area=function(){ return this.a*this.b; //console.log(math.pi*this.r*this.r); } this.premeter=function(){ return (this.a+this.b)*2; //console.log(math.pi*this.r*2); } }else{ return new retangle(a,b); } } let c=new retangle(10,10); //console.log(c instanceof circle);//判断bool值 console.log(c.area()); console.log(c.premeter()); retangle(10,10); </script></head><body></body></html>
圆的面积:
<!doctype html><html><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <title>document</title> <script> function circle(r){ if(this instanceof circle){ this.r=r; this.area=function(){ return math.pi*this.r*this.r; } this.premeter=function(){ return math.pi*this.r*2; } }else{ return new circle(r); } } let c=new circle(10); //console.log(c instanceof circle); console.log(c.area()); console.log(c.premeter()); circle(10); </script></head><body> </body></html>
输出的时候有两种形式:
windows-普通函数
circle(10);console.log(window.r);console.log(window.area());console.log(window.premter());
构造函数
let c = new circle(10);// console.log(c);console.log(c instanceof circle); console.log(c.r);console.log(c.area());console.log(c.premter());
第二种:
<!doctype html><html><head> <meta charset="utf-8"> <title>title</title> <script> function retangle(){ var a = document.getelementbyid("a1").value; var b = document.getelementbyid("b1").value; alert("矩形面积:"+a*b); } </script></head><body><form> 长:<br> <input type="text" id="a1"><br> 宽:<br> <input type="text" id="b1"><br> <button onclick="retangle()" name="矩形面积" value="">计算面积</button> <input type="reset" name="reset" value="重置"></form></body></html>
推荐学习:javascript视频教程
以上就是javascript如何求图形的面积的详细内容。