ES5构造函数继承

 

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <script>
            function Phone(brand,price){
                this.brand = brand
                this.price = price
            }
            
            Phone.prototype.call = function(){
                console.log("我可以打电话");
            }
            
            // 智能手机
            function SmartPhone(brand,price,color,size){
                Phone.call(this,brand,price)
                this.color = color;
                this.size = size;
            }
            
            // 设置子级构造函数的原型
            SmartPhone.prototype= new Phone;
            SmartPhone.prototype.constructor = SmartPhone;
            
            // 声明子类的方法
            SmartPhone.prototype.photo = function(){
                console.log('我可以拍照');
            }
            
            SmartPhone.prototype.playGame = function(){
                console.log('我可以拍照');
            }
            const chuizi = new SmartPhone('锤子',2499,'黑色','5.5inch')
        
            console.log(chuizi);
        </script>
    </body>
</html>

 

 

posted @ 2023-02-20 11:21  罗砂  阅读(17)  评论(0)    收藏  举报