最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is there a benefit to use "hasOwnProperty()" when iterating through object keys in JavaScript - Stack Overflow

programmeradmin0浏览0评论

I'm trying to understand the goal of using hasOwnProperty() check when iterating through object keys. As I know, the iteration will be executed for every object property (not more, not less) in both cases: with hasOwnProperty() or without. For example, in code below results with hasOwnProperty() check and without are the same:

const obj = {
  prop1: [],
  prop2: {},
  prop3: "",
  prop4: 0,
  prop5: null,
  prop6: undefined
}

const resultWithHasOwnProperty = [];
const resultWithoutHasOwnProperty = [];

for(key in obj) {
  if(obj.hasOwnProperty(key)) {
    resultWithHasOwnProperty.push(obj[key])
  }
}

for(key in obj) {
  resultWithoutHasOwnProperty.push(obj[key])
}

console.log("Result WITH hasOwnProperty check: ", resultWithHasOwnProperty);
console.log("Result WITHOUT hasOwnProperty check: ", resultWithoutHasOwnProperty);

I'm trying to understand the goal of using hasOwnProperty() check when iterating through object keys. As I know, the iteration will be executed for every object property (not more, not less) in both cases: with hasOwnProperty() or without. For example, in code below results with hasOwnProperty() check and without are the same:

const obj = {
  prop1: [],
  prop2: {},
  prop3: "",
  prop4: 0,
  prop5: null,
  prop6: undefined
}

const resultWithHasOwnProperty = [];
const resultWithoutHasOwnProperty = [];

for(key in obj) {
  if(obj.hasOwnProperty(key)) {
    resultWithHasOwnProperty.push(obj[key])
  }
}

for(key in obj) {
  resultWithoutHasOwnProperty.push(obj[key])
}

console.log("Result WITH hasOwnProperty check: ", resultWithHasOwnProperty);
console.log("Result WITHOUT hasOwnProperty check: ", resultWithoutHasOwnProperty);

So my question is: why and when should we use hasOwnProperty() check?

I'll rephrase my question: without hasOwnProperty() check we will always iterate through all existing properties anyway?

Notice, this question is not really a duplicate.

Share Improve this question edited Nov 16, 2017 at 12:09 P.S. asked Sep 29, 2017 at 0:54 P.S.P.S. 16.4k14 gold badges65 silver badges86 bronze badges 7
  • 1 Possible duplicate of Javascript what is property in hasOwnProperty? – samnu pel Commented Sep 29, 2017 at 0:56
  • I'll rephrase my question: without hasOwnProperty() check we will always iterate through all existing properties anyway? – P.S. Commented Sep 29, 2017 at 0:58
  • 1 Good article here: adripofjavascript./blog/drips/… – jmargolisvt Commented Sep 29, 2017 at 0:58
  • @jmargolisvt, thanks for the link ;) – P.S. Commented Sep 29, 2017 at 1:00
  • 2 See Can I finally let hasOwnProperty() die in for loops? – Bergi Commented Sep 29, 2017 at 1:40
 |  Show 2 more ments

1 Answer 1

Reset to default 10

The docs indicate that:

The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as own (not inherited) property.

The hasOwnProperty method ensure that the property you are checking is directly on an instance of an object but not inherited from its prototype chain. If you don't check, it will loop through every property on the prototype chain.

const obj = {
  prop1: [],
  prop2: {},
  prop3: "",
  prop4: 0,
  prop5: null,
  prop6: undefined
}

obj.prototype = {foo: 'bar'};

P/s: NOTE that:

JavaScript does not protect the property name hasOwnProperty; thus, if the possibility exists that an object might have a property with this name, it is necessary to use an external hasOwnProperty to get correct results:

var foo = {
  hasOwnProperty: function() {
    return false;
  },
  bar: 'Here be dragons'
};

foo.hasOwnProperty('bar'); // always returns false

So you need to use:

Object.prototype.hasOwnProperty.call(foo, 'bar');

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论