以下を使用して、名前空間内にプロトタイプを割り当てようとしています (すべてがグローバル オブジェクトにラップされています)。
プロトタイプオブジェクト: {
Person : function(config){
var that = this;
this.name = config.name;
this.age = config.age;
console.log(that);
that.prototype.working = function(){
console.log(this.name + 'is working');
};
},
}、
次に、コンソールでこれを使用して確認しています。
var me = new global.prototypeObjects.Person({name:'Mike', age:'40'});
このエラーが発生します:
TypeError: 未定義のプロパティ 'working' を設定できません
ただし、プロトタイプの割り当てを明示的に行う場合、つまり:
プロトタイプオブジェクト: {
Person : function(config){
var that = this;
this.name = config.name;
this.age = config.age;
console.log(that);
**global.prototypeObjects.Person**.prototype.working = function(){
console.log(this.name + 'is working');
};
}
},
その後、期待どおりに動作し、次の結果が得られます。
global.prototypeObjects.Person {name: "Mike", age: "40", working: function}
そして me.working() は「Mike is working」をログアウトします
この例で「これ」を使用できない理由を誰か説明できますか?