4

I knew that inherited properties are not enumerable. In the following code stanford inherits a property level from another object university.

But when I enumerate stanford, I see that the inherited property also being listed. Please explain the matter (i must be wrong somewhere).

var university = {level:"bachelor"};

var stanford = Object.create(university);
stanford.country = "USA";
stanford.rating = "Good";

console.log(Object.hasOwnProperty("level"));    //  false

for(var properties in stanford){                //  level, country, rating
    console.log(properties);
}
Sajib Biswas
  • 433
  • 4
  • 13
  • 1
    Because `for..in` iterates over all properties in the prototype chain. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in . – Felix Kling Dec 26 '15 at 10:35

3 Answers3

1

Such a wonderful link!! Thanks @Sajib Biswas for this question.

Refer object-getownpropertynames-vs-object-keys

and how-do-i-enumerate-the-properties-of-a-javascript-object

I think This may help :

var proto = Object.defineProperties({}, {
  protoEnumTrue: { value: 1, enumerable: true },
  protoEnumFalse: { value: 2, enumerable: false }
});
var obj = Object.create(proto, {
  objEnumTrue: { value: 1, enumerable: true },
  objEnumFalse: { value: 2, enumerable: false }
});

for (var x in obj) console.log(x);

Results: 

objEnumTrue
protoEnumTrue


console.log(Object.keys(obj)); // ["objEnumTrue"]
console.log(Object.getOwnPropertyNames(obj)); //  ["objEnumTrue", "objEnumFalse"]

The enumeration will return properties not just of the object being enumerated, but also from the prototypes of any parent objects.

Object.getOwnPropertyNames(a) returns all own properties of the object a. Object.keys(a) returns all enumerable own properties.

From Your Example:

var university = {level:"bachelor"};

var stanford = Object.create(university);
stanford.country = "USA";
stanford.rating = "Good";

console.log(university.hasOwnProperty("level"));    //  true
console.log(university.hasOwnProperty("country"));    //  false

console.log(stanford.hasOwnProperty("country"));    //  true
console.log(stanford.hasOwnProperty("level"));    //  false
Community
  • 1
  • 1
saravanakumar
  • 1,747
  • 4
  • 20
  • 38
1

which properties are not enumerable?

Javascript has two types of object properties: Data properties and Accessor properties. In your case you are working with data properties. To manipulate enumeration of certain property use property descriptor with Object.defineProperty() method.
Example:

 var stanford = Object.create(university);
    stanford.country = "USA";
    stanford.rating = "Good";
    Object.defineProperty(stanford, 'rating', {
        enumerable: false
    });

 console.log(Object.keys(stanford)); // Now you won't see 'rating' here
 // the same effect takes place for loop operators (for;;) , for .. in ..)
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
1

This is the structure of your stanford-object:

enter image description here

As you can see it inherits the other Object wich has the property level, but does not "hasOwnProperty" level. However for..in will loop over all properties.

CoderPi
  • 12,985
  • 4
  • 34
  • 62