この質問はUsing "Object.create" instead of "new"の重複ではありません。問題のスレッドは、使用時に引数を正しく渡すことに焦点を当てていませんObject.create
Object.create
ではなく を使用してオブジェクトを初期化する方法に興味がありますnew
。これまでの私のコードは次のとおりです。
function Human(eyes) {
this.eyes = eyes || false;
}
Human.prototype.hasEyes = function() {
return this.eyes;
}
function Male(name) {
this.name = name || "No name";
}
Male.prototype = new Human(true); //passing true to the Human constructor
var Sethen = new Male("Sethen");
console.log(Sethen.hasEyes());
上からわかるように、 はMale.prototype = new Human(true);
true で新しいオブジェクトを作成します。hasEyes()
関数が実行されると、期待どおりに true が記録されます 。
それで、私の質問は..パラメータObject.create
を渡すのと同じ方法でこれを行うにはどうすればよいですかtrue
??