function ClassA(color){ this.color = color; this.sayColor = function(){ console.log(this.color); } } function ClassB(name){ this.name = name; this.sayName = function(){ console.log(this.name) } } //通过对ClassA、ClassB使用CALL方法,改变其THIS的指向,让其this指向被实例化的C这个新对象,从而让C的实例能够创建出A和B的方法和属性 function ClassC(price,scolor,sname){ ClassA.call(this,scolor); //实现继承 ClassB.call(this,sname); //实现继承
this.price = price; this.price = function(){ console.log(this.price); } } var oC = new ClassC(1200,"red","anyCall"); oC.price(); oC.sayColor(); oC.sayName();
通过对ClassA、ClassB使用CALL方法,改变其THIS的指向,让其this指向被实例化的C这个新对象,从而让C的实例能够创建出A和B的方法和属性(APPLY方法和CALL方法,只是后面传的参数有区别)
本文通过具体示例,展示了在JavaScript中如何使用call方法实现类的多重继承,使得一个类可以继承多个父类的属性和方法。
1635

被折叠的 条评论
为什么被折叠?



