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>

相关文章
相关标签/搜索