function object(o){
function F(){}
F.prototype = o;
return new F();
}
function inheritPrototype(subType, superType){
var prototype = object(superType.prototype);
prototype.constructor = subType;
subType.prototype = prototype;
}
function SuperType(name){
this.name = name;
this.clolors = ['red', 'blue', 'green'];
}
SuperType.prototype.sayName = function (){
alert(this.name);
}
function SubType(name, age){
SuperType.call(this, name);
this.age = age;
}
SubType.prototype = inheritPrototype(subType, superType);
SuperType.prototype.sayAge = function (){
alert(this.age);
}
这里是高程里实现寄生组合式继承的方法,我有一点想不明白:
function object(o){
function F(){}
F.prototype = o;
return new F();
}
function inheritPrototype(subType, superType){
var prototype = object(superType.prototype);
prototype.constructor = subType;
subType.prototype = prototype;
}
这两个函数实现的效果和subType.prototype.__proto__ = superType.prototype有什么区别呢?如果没有区别,为什么非要用两个函数来实现?
心有法竹
相关分类