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