javascript--继承(对象冒充的多重继承)

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方法,只是后面传的参数有区别)

posted @ 2012-05-28 12:42  o0Luffy0o  阅读(733)  评论(0编辑  收藏  举报