違いが頭に浮かんだと思いますが、念のため申し上げます。
Douglas CrockfordのページでJavaScriptのプロトタイプの継承について、彼は言います
プロトタイプシステムでは、オブジェクトはオブジェクトから継承します。ただし、JavaScriptには、その操作を実行する演算子がありません。代わりに、new f()がf.prototypeから継承する新しいオブジェクトを生成するように、new演算子があります。
私は彼がその文で何を言おうとしているのか本当に理解していなかったので、私はいくつかのテストを行いました。主な違いは、純粋なプロトタイプシステムで別のオブジェクトに基づいてオブジェクトを作成する場合、すべての親の親メンバーは、新しいオブジェクト自体ではなく、新しいオブジェクトのプロトタイプ上にある必要があるということです。
テストは次のとおりです。
var Person = function(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.toString = function(){return this.name + ', ' + this.age};
// The old way...
var jim = new Person("Jim",13);
for (n in jim) {
if (jim.hasOwnProperty(n)) {
console.log(n);
}
}
// This will output 'name' and 'age'.
// The pure way...
var tim = Object.create(new Person("Tim",14));
for (n in tim) {
if (tim.hasOwnProperty(n)) {
console.log(n);
}
}
// This will output nothing because all the members belong to the prototype.
// If I remove the hasOwnProperty check then 'name' and 'age' will be output.
オブジェクト自体のメンバーをテストするときにのみ違いが明らかになるという私の理解は正しいですか?