私は現在、プロトタイプの継承が JavaScript でどのように機能するかを正確に把握しようとしています。これが私が現在解決しようとしている謎です。
次の構造を設定するとします。
var base = { greet : "hello" }
var child = function () {}
child.prototype = base;
child.prototype.protoSomething = function () {}
var instance = new child();
派手なものはありません。instance
次に、プロパティ (所有またはその他) の内容を見てみましょう。
for(prop in instance) {
console.log(prop); // prints 'greet', 'protoSomething'
}
よし、それはgreet
とprotoSomething
を持っている、彼らinstance
自身のメンバーですか?
for(prop in instance) {
if(instance.hasOwnProperty(prop))
console.log(prop); // nothing is printed
}
いいえ、自分のプロパティ リストは空です。彼らはinstance
のプロトタイプに入っていますか?
if(instance.prototype === undefined) {
console.log("'instance' has no prototype"); // gets printed
}
残念ながら、instance
プロトタイプが割り当てられていません。では、プロパティが独自のものではなく、プロトタイプがない場合、それらはどこから来ているのでしょうか? この時点で、ある種の図がとてもいいと思います。