js call

要求:
在Autobots構造函數中使用call借用Car構造函數實現name屬性的繼承
在Car構造函數的原型中添加一個run方法,並經過原型繼承給Autobots構造函數的實例對象
但願Autobots構造函數建立的實例對象可以擁有變形(distort方法)的能力html

注意:普通汽車不能變形,汽車人是能夠變形的。
    汽車人對象將會有name,color屬性,並且擁有run,distort方法
    其中name,distort都是經過繼承得來的。
<!DOCTYPE html>
<html lang="en">
<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>
      
     //建立car構造函數
     function Car(name){
         this.name=name;
         //原型中添加方法不肯定這樣對不對
         this.distort=function(){
         console.log("我還會變形");
      }
     }


 
     //建立Autobots構造函數
     function Autobots(color){
         this.color=color;
        //繼承car,同時還傳遞參數
        Car.call(this,"我是大黃蜂");
        //添加變形方法
        this.run=function(){
            console.log("不只會飛,還會跑");
        }
     }
      
    //實例化傳入color
     var Deformation = new Autobots("黃色");
     //經過原型繼承run方法
     Autobots.prototype.distort;
     //打印
     console.log(Deformation.name+Deformation.color);
     Deformation.run()
     Deformation.distort();
    </script>
</head>
<body>
    
</body>
</html>

相關文章
相關標籤/搜索