[英]javascript hasOwnProperty returns true instead of false?
我不明白為什么警報(John.hasOwnProperty('firstName')); 返回true,而firstName是在Person原型中定義的,而不是在John實例中定義的?
https://jsfiddle.net/xfdnsg2w/
Person = function(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
var John = new Person("John");
alert(John.hasOwnProperty('toString'));
alert(John.hasOwnProperty('firstName'));
在你的代碼中的“名字”屬性沒有在人物原型定義。 它在構造函數中初始化為“ own”屬性。
即使原型上具有“ firstName”和“ lastName”屬性,只要在構造函數中為它們分配值,它們也將立即成為“所有者”屬性。 原型屬性通常用作訪問的屬性,並且通常具有作為值的功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.