Madhur Ahuja
Madhur Ahuja

Reputation: 22671

Listing the properties of Object.prototype

I am trying to print out the properties of Object.prototype but keep getting undefined as a result. Can anyone tell me what I am doing wrong ?

for (var property in Object.prototype) {
    if (Object.prototype.hasOwnProperty(property)) {
        console.log(property);

    }
}

undefined

Upvotes: 0

Views: 34

Answers (2)

Scimonster
Scimonster

Reputation: 33399

meager's answer is correct, but i will explain how to do what you wanted here.

You need to use Object.getOwnPropertyNames to get a list.

var properties = Object.getOwnPropertyNames(Object.prototype);
for (var i=0; i<properties.length; i++) {
    if (Object.prototype.hasOwnProperty(properties[i])) {
        console.log(properties[i]);
    }
}

Upvotes: 1

user229044
user229044

Reputation: 239290

You're not "getting undefined", your loop is simply executing 0 times, and your JavaScript console's REPL is showing you the value of the last statement is "undefined".

Object.prototype has no enumerable properties.

Upvotes: 2

Related Questions