admin管理员组

文章数量:1390604

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');

本文标签: