LearningMath
LearningMath

Reputation: 861

Why Object.getOwnPropertyNames() gives the methods on the prototype too?

var properties = Object.getOwnPropertyNames(Array); 
console.log(properties);

Why does this code list the properties of the Array.prototype object too? According to my understanding it should list these properties that are found directly on the Array object:

Array.length
Array.constructor
Array.prototype
Array.isArray
Array.of
Array.observe
Array.from
Array.unobserve

Upvotes: 0

Views: 349

Answers (2)

Bergi
Bergi

Reputation: 665276

You must be on Firefox. It does implement a non-standard extension of the Array constructor, where generics function are available as static methods on Array itself. They are own properties, with the same names as those on Array.prototype but different values. They are not present in other browsers and won't be in future Firefoxes either.

Upvotes: 1

zer00ne
zer00ne

Reputation: 44088

It is functioning correctly.

Object.getOwnPropertyNames() returns an array whose elements are strings corresponding to the enumerable and non-enumerable properties found directly upon obj....

MDN

var propertiesOfArray = Object.getOwnPropertyNames(Array);

console.log(propertiesOfArray);

var propertiesOfArrayPrototype = Object.getOwnPropertyNames(Array.prototype);

console.log(propertiesOfArrayPrototype);

If the properties of Array.prototype is what you want, then don't use Array, use Array.prototype. See update. And the accepted answer here.

Demo

Upvotes: 1

Related Questions