我有點困惑關於正在發生的事情與我的代碼,這裏是我到目前爲止有:對象原型方法與重視
function Person(name) {
this.name = name;
}
Person.prototype.getName = this.name;
Person.prototype.displayName = function() {
return this.name;
}
var Sethen = new Person("Sethen");
console.log(Sethen.getName);
console.log(Sethen.displayName());
我很好奇,爲什麼getName
不給我this.name
值,而只記錄的空白,而displayName
方法給我正確的值。 getName
是我原型對象的一個屬性,所以我的思考過程是我可以像這樣抓住它。
爲什麼getName
無法登錄的Sethen
價值?我會如何去抓住像普通財產那樣的信息?我必須使用一種方法嗎?
如果你打算downvote給出一個理由。建設性地爲這個社區做出貢獻。 – Sethen