JavaScriptの継承について混乱しています。Javascript継承との混乱
は、次のコードを考えてみましょう:今すぐ
function parent(firstname, lastname) {
this.firstname = firstname || "abc";
this.lastname = lastname || "def";
}
function child() {
this.childname = "xys";
}
parent.prototype.Greetings = function() {
alert("sayhi");
}
child.prototype = Object.create(parent.prototype);
var child1 = new child();
を、child1
オブジェクトがfirstname
とlastname
プロパティへのアクセス権を持っていますか? Greetings
メソッドにアクセスできます(プロトタイプに含まれているため)。 これらにアクセスしようとすると、undefined
と表示されます。 これらの変数にアクセスするためにはどのような変更を行う必要がありますか?
注: 'object.create'ではなく' Object.create'です。 –
編集のための@melpomene – Geeky