Javascript hasOwnProperty中的属性是什么?

FLY*_*FLY 69 javascript object hasownproperty

if (someVar.hasOwnProperty('someProperty') ) {
 // do something();
} else {
 // do somethingElse();
}
Run Code Online (Sandbox Code Playgroud)

什么是正确的使用/解释hasOwnProperty('someProperty')

为什么我们不能简单地someVar.someProperty用来检查对象是否someVar包含具有名称的属性someProperty

在这种情况下,什么是财产?

这个javascript检查什么属性?

Jam*_*ice 118

hasOwnProperty返回一个布尔值,指示您调用它的对象是否具有带参数名称的属性.例如:

var x = {
    y: 10
};
console.log(x.hasOwnProperty("y")); //true
console.log(x.hasOwnProperty("z")); //false
Run Code Online (Sandbox Code Playgroud)

但是,它不会查看对象的原型链.

当您使用for...in构造枚举对象的属性时,使用它很有用.

如果您想查看完整的详细信息,ES5规范一如既往是一个好看的地方.

  • 原型链的奖励积分.仍然试图弄清楚当它没有被调用对象时它的调用是什么......它不是"窗口" (4认同)

Om *_*Sao 18

这是简短而准确的答案:

在javascript中,每个对象都有一堆内置的键值对,这些键值对包含有关对象的元信息.当你使用for...in构造/ 循环遍历所有键值对时,你也会循环遍历这个元信息键值对(你绝对不想要).

在此输入图像描述

使用hasOwnPropery(property) 过滤器 -通过元信息进行这些不必要的循环并直接检查参数property是否是用户给定的属性.通过过滤器,我的意思是,hasOwnProperty(property)看起来不property存在于Object的原型链中,即meta信息.

true/false基于此返回布尔值.

这是一个例子:

var fruitObject = {"name": "Apple", "shape": "round", "taste": "sweet"};
console.log(fruitObject.hasOwnProperty("name"));  //true
console.log(Object.prototype.hasOwnProperty("toString");) //true because in above snapshot you can see, that there is a function toString in meta-information
Run Code Online (Sandbox Code Playgroud)

我希望它很清楚!

  • >“你也在循环遍历这个元信息键值对”但是当我运行 `for (var key infruitObject) { ... }` js 无论如何都只循环遍历非原型键时,我是否遗漏了一些东西或者JS 运行时是否改变了它们处理 key-in-object 循环的方式? (2认同)

Pra*_*ana 11

它检查:

返回一个布尔值,指示对象是否具有指定名称的属性

hasOwnProperty如果对象具有指定名称的属性,假如果不返回true.此方法不检查对象的原型链中是否存在该属性; 该属性必须是对象本身的成员.

示例:

var s = new String("Sample");
document.write(s.hasOwnProperty("split"));                        //false 
document.write(String.prototype.hasOwnProperty("split"));         //true
Run Code Online (Sandbox Code Playgroud)

  • 我给了 -1,因为你最初的答案是一个简短且完全不连贯的句子,然后更新为一个稍长、稍有连贯但完全不准确的句子。 (2认同)

Wil*_*een 8

摘要:

hasOwnProperty()是可以在任何对象上调用的函数,并且将字符串作为输入。true如果属性位于对象上,则返回一个布尔值,否则返回false。hasOwnProperty()位于Object.prototype任何对象上,因此可用于任何对象。

例:

function Person(name) {
  this.name = name;
}

Person.prototype.age = 25;

const willem = new Person('willem');

console.log(willem.name); // property found on object
console.log(willem.age); // property found on prototype

console.log(willem.hasOwnProperty('name')); // name is on the object itself
console.log(willem.hasOwnProperty('age')); // age is not on the object itself
Run Code Online (Sandbox Code Playgroud)

在此示例中,创建了一个新的Person对象。每个人都有自己的名称,该名称在构造函数中初始化。但是,年龄不是位于对象上,而是位于对象的原型上。因此,hasOwnProperty()确实会返回true姓名和false年龄。

实际应用:

hasOwnProperty()使用循环在对象上循环时非常有用for in。您可以检查它的属性是否来自对象本身而不是原型。例如:

function Person(name, city) {
  this.name = name;
  this.city = city;
}

Person.prototype.age = 25;

const willem = new Person('Willem', 'Groningen');

for (let trait in willem) {
  console.log(trait, willem[trait]); // this loop through all properties including the prototype
}

console.log('\n');

for (let trait in willem) {
  if (willem.hasOwnProperty(trait)) { // this loops only through 'own' properties of the object
    console.log(trait, willem[trait]);
  }
}
Run Code Online (Sandbox Code Playgroud)