Home > Web Front-end > JS Tutorial > body text

How to find the area of ​​a graphic in javascript

王林
Release: 2021-10-26 16:21:04
Original
2711 people have browsed it

Javascript method to find the area of ​​a graphic: [function Circle(r){ if(this instanceof Circle){this.r=r; this.area=function(){return Math.PI*this].

How to find the area of ​​a graphic in javascript

#The operating environment of this article: windows10 system, javascript 1.8.5, thinkpad t480 computer.

Graphic area solution:

The first type: constructor

<!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>
Copy after login

The area of ​​the circle:

<!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>
Copy after login

There are two forms of output:

Windows-Common function

Circle(10);
console.log(window.r);
console.log(window.area());
console.log(window.premter());
Copy after login

Constructor

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());
Copy after login

Second type:

<!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>
Copy after login

Recommended learning:javascript video tutorial

The above is the detailed content of How to find the area of ​​a graphic in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template