JavaScript继承圣杯模式
<script>
function inherit(Target, Origin){
var F = function(){};
F.prototype = Origin.prototype;
Target.prototype = new F();
// son.__proto__ --> new F().__proto__ --> Father.__proto__
Target.prototype.constructor = Target;
//构造出的对象, 能找到自己的超类,超级父级 (究竟继承自谁)
Target.prototype.uber = Origin.prototype;
}
inherit(Son,Father);
Father.prototype.name = "zhang";
function Father(){};
function Son(){};
var son = new Son();
var father = new Father();
</script>
雅虎将此方法封装到YUI3库
var inherit = (function(){
var F = function(){};
return function(Target, Origin){
F.prototype = Origin.prototype;
Target.prototype = new F();
Target.prototype.constructor = Target;
Target.prototype.uber = Origin.prototype;
}
}());