私は本でJavaScriptを再生しますウェブデベロッパーのためのプロフェッショナルJavaScript。私は6.2.6節の例を練習します。コードは次のとおりです。Javascript:[ユーザー定義のプロトタイプ]からの変数は利用できません
function creatPrototype(subType, superType)
{
function subTypePrototype(){};
subTypePrototype.prototype = superType.prototype;
subTypePrototype.constructor = subType;
subTypePrototype.str = "say";
return new subTypePrototype();
}
function Person(name, age)
{
this.name = name;
this.age = age;
}
Person.prototype.say = function(){
writeln("bill say");
}
function itMan(name, age){
Person.apply(this, arguments);
}
itMan.prototype = creatPrototype(itMan, Person);
var Bill = new itMan("bill", 25);
writeln(itMan.prototype.str); //expect "say"
writeln(Person.prototype == itMan.prototype.prototype); //expect true
Bill.say(); //expect "bill say"
結果は次のとおりです。
未定義
偽
法案を
なぜ言いますか?
itMan.prototype.strは
Person.prototypeを "言う" と仮定されてとitMan.prototype.prototypeが同じオブジェクト
Bill.sayを指す必要があります( )正しく実行されるので、プロトタイプチェーンはOKです。コードといくつかの誤りがありました
あなたはポイントを持っています。あなたのソリューションは**本の**の元の例に似ています。 _subTypePrototype。constructor_とstrは戻り値の一部ではないかもしれません_new subTypePrototype()_。 – delai