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
1 Answer
Reset to default 10The 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');
本文标签:
版权声明:本文标题:Is there a benefit to use "hasOwnProperty()" when iterating through object keys in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744706470a2620870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论