javascript如何求图形的面积

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视频教程

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...