js call

要求:
在Autobots构造函数中使用call借用Car构造函数实现name属性的继承
在Car构造函数的原型中添加一个run方法,并通过原型继承给Autobots构造函数的实例对象
希望Autobots构造函数创建的实例对象能够拥有变形(distort方法)的能力

    注意:普通汽车不能变形,汽车人是可以变形的。
    汽车人对象将会有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>

原文地址:https://www.cnblogs.com/shapaozi/p/10544685.html