JavaScript 实现继承 —— { }

<script>
    // 继承
    function Shape(name){
    
    
        this.name = name;
    }

    function circle(name,radiu){
    
    
        Shape.call(this,name);
        this.radiu = radiu;
    }

    circle.prototype = new Shape();
    circle.prototype.constructor = circle;
    circle.prototype.computeArea = function(){
    
    
        console.log("圆的面积:" + this.radiu * this.radiu * Math.PI);
    }

    let c1 = new circle('圆形1',2);
    console.log(c1);
    c1.computeArea();
</script>

猜你喜欢

转载自blog.csdn.net/weixin_43921423/article/details/113839638