对象冒充可以支持多重继承:
function classZ(){
this.newMethod = classX;
this.newMethod();
delete this.newMethod;

this.newMethod = classY;
this.newMethod();
delete this.newMethod;
}
这里存在一个弊端,如果ClassX和ClassY具有同名的属性或方法,ClassY具有高优先级,因为继承的是最后的类。除这点小问题之外,用对象冒充实现多继承机制轻而易举。由于这种继承方法的流行,ECMAScript的第三版为Function对象加入了两个新方法,即call()和apply()。
call()方法
call()方法是与经典的对象冒充方法最相似的方法。它的第一个参数用作this的对象。其他参数都直接传递给函数自身。例如:
function sayColor(sPrefix,sSuffix){
alert(sPrefix + this.color + sSuffix);
}
var obj = new Object();
obj.color = 'red';
//The color is red, a very nice color indeed.
sayColor.call(obj,'The color is ',', a very nice color indeed.');
第一个参数是obj,说明应该赋予sayColor()函数中的this关键字值是obj。第二个和第三个参数是字符串。它们与sayColor()函数中的参数prefix和suffix匹配。
要与继承机制的对象冒充方法一起使用该方法,只需将前三行的赋值、调用和删除代码替换即可:
function classB(sColor, sName){
//this.newMethod = classA;
//this.newMethod(sColor);
//delete this.newMethod; ClassA.call(this,sColor);
this.name = sName;
this.sayName = function(){
alert(this.name);
}
}