javascript怎么求图形的面积

发布时间:2021-10-26 16:37:06 作者:iii
来源:亿速云 阅读:141

这篇文章主要讲解了“javascript怎么求图形的面积”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“javascript怎么求图形的面积”吧!

javascript求图形的面积的方法:【function Circle(r){ if(this instanceof Circle){this.r=r; this.area=function(){return Math.PI*this】。

javascript怎么求图形的面积

本文操作环境: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怎么求图形的面积这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. php制作图形面积计算器(代码)
  2. 利用Python求阴影部分的面积实例代码

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

javascript

上一篇:把Python嵌入C++的运算符重载的操作步骤是什么

下一篇:vuejs中data与props有哪些区别

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》