原型继承(圣杯模式)
function inherit(Target,Origin){
function F(){};//中间层
F.prototype = Origin.prototype;
Target.prototype = new F();
Target.prototype.constuctor = Target;
Target.prototype.uber = Origin.prototype//便于直接找到Target继承自谁
}
Father.prototype.lastName = 'yang';
function Father(){
};
function Son(){
};
inherit(Son,Father);
var father = new Father();
var son = new Son();
本文深入探讨了JavaScript中的一种原型继承模式——圣杯模式。通过定义中间层构造函数,将目标对象的原型指向原始对象的实例,同时修正构造函数指向,确保继承链正确。此外,还介绍了如何使用uber属性方便地引用父类原型。
275

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



