最近、オブジェクト指向プログラミング、継承とプロトタイプコンストラクタの使用は、相続または少し小さな欠陥の組み合わせを発見したので、再び高度なオブジェクト指向プログラミング章VIを再び見て、継承された寄生の組み合わせは本当に良いが、彼らが共有することがわかったJS皆。
function object( o ) {
function F () {};
F.prototype = o;
return new F()
};
function inheritPrototype ( SubType, SupType ) {
var prototype = object ( supType.prototype );
prototype.constructor = SubType;
SubType.prototype = prototype;
};
function SupType ( name ) {
this.name = name;
this.friends = [ "a", "b", "c" ];
};
SupType.prototype.sayName = function () {
alert( this.name );
};
function SubType ( name, age ) {
SupType.call( this, name );
this.age = age;
};
inheritPrototype( SubType, SupType );
SubType.prototype.sayAge = function () {
alert( this.age );
};