我是靠谱客的博主 饱满凉面,这篇文章主要介绍javascript如何求图形的面积,现在分享给大家,希望可以做个参考。

本文操作环境:windows10系统、javascript 1.8.5、thinkpad t480电脑。

图形面积求解:

第一种:构造函数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!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>
登录后复制

圆的面积:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<!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-普通函数

复制代码
1
2
3
4
Circle(10); console.log(window.r); console.log(window.area()); console.log(window.premter());
登录后复制

构造函数

复制代码
1
2
3
4
5
6
7
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());
登录后复制

第二种:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!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如何求图形的面积的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是饱满凉面最近收集整理的关于javascript如何求图形的面积的全部内容,更多相关javascript如何求图形内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(96)

评论列表共有 0 条评论

立即
投稿
返回
顶部