0

Having this code

var a = {date : 12};
for(var t in a){
    alert(t);
}

I have one alert with date property.

But everywhere (i.e. here) i see that it is needed to write :

for (var property in object) {
    if (object.hasOwnProperty(property)) {
        // do stuff
    }
}

So why i don't see internal properties of object?

Community
  • 1
  • 1
  • What do you mean by 'internal properties'. In your last example you just make an edge test if the property you're looking for actually exists in 'your object'. –  Jul 31 '15 at 09:34
  • which internal properties do you want to see? – Iván Rodríguez Torres Jul 31 '15 at 09:34
  • 1
    Op says _"why it's required to use `hasOwnProperty`, because i don't see internal props even i didn't use `hasOwnProperty`, so why do i need it?"_ IMHO – Medet Tleukabiluly Jul 31 '15 at 09:42
  • if in console i'll create var a = {a : 1}; and on the next line will type a. i'll see a lot of properties: constructor, isPrototypeOf ... and i guess hasOwnProperty used exactly for the case when i don't want to iterate through them. – Valentyn Vynogradskiy Jul 31 '15 at 10:02

4 Answers4

0

In JavaScript there exists the 'prototype chain', a form of inheritance. If you were to add a property to the prototype of a, and then add your date property directly onto a, your for loop would find both those properties.

The hasOwnProperty checks whether the current property has been attached directly to the context object or whether it has been inherited through the prototype chain.

So in my example, if you had added a property to a's prototype, the hasOwnProperty condition would return false and you wouldn't "do stuff" with it.

awj
  • 7,482
  • 10
  • 66
  • 120
0

The for..in syntax iterates every property of the object, including those found on the prototype chain (if they are enumerable). This is usually not what you want, so the hasOwnProperty check is usually recommended.

In your case it's not necessary, as your plain object doesn't inherit any enumerable properties from the Object prototype (the only thing in its prototype chain)

Also, for..in gives you the property names (keys) of the object, if you're interested in the value of that particular property, you have to use the bracket notation, extending your example to this:

var a = {date : 12};
for(var t in a){
    alert(a[t]);
}
doldt
  • 4,466
  • 3
  • 21
  • 36
  • what about system fields for every js object? isPrototypeOf, constructor, __defineGetter__? Don't i have to see 'em? – Valentyn Vynogradskiy Jul 31 '15 at 10:07
  • 1
    What do you mean by "have to see them"? They are non-enumerable, so they won't show up when you do a for..in iteration, with or without the hasOwnProperty check. But you *can* reference them directly, if you need to. – doldt Jul 31 '15 at 11:33
0

I guess the best way to see it is through an example:

Object.prototype.aBaseProperty = 123;

var childObject = { date: 12 };

for (var property in childObject) {
    console.log(property)
}

// output: date aBaseProperty


for (var property in childObject) {
    if (childObject.hasOwnProperty(property)) {
        console.log(property)
    }
}

// output:  date
Daniel Conde Marin
  • 7,588
  • 4
  • 35
  • 44
0

Also, rather than doing the hasOwnProperty check, you might prefer

Object.keys(object).forEach(function(value, key) { ... });

Where Object.keys will only return own properties.

Jacob
  • 1,335
  • 1
  • 14
  • 28