Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the non-enumerable inherited property names of an object?

In JavaScript we have a few ways of getting the properties of an object, depending on what we want to get.

1) Object.keys(), which returns all own, enumerable properties of an object, an ECMA5 method.

2) a for...in loop, which returns all the enumerable properties of an object, regardless of whether they are own properties, or inherited from the prototype chain.

3) Object.getOwnPropertyNames(obj) which returns all own properties of an object, enumerable or not.

We also have such methods as hasOwnProperty(prop) lets us check if a property is inherited or actually belongs to that object, and propertyIsEnumerable(prop) which, as the name suggests, lets us check if a property is enumerable.

With all these options, there is no way to get a non-enumerable, non-own property of an object, which is what I want to do. Is there any way to do this? In other words, can I somehow get a list of the inherited non-enumerable properties?

Thank you.

like image 941
dkugappi Avatar asked Nov 05 '11 23:11

dkugappi


People also ask

What are non-enumerable properties?

Objects can have properties that don't show up when iterated through the particular object using Object. keys() or for...in loop. Those type of properties are called as non-enumerable properties.

How do you get the properties name of an object in typescript?

To dynamically access an object's property: Use keyof typeof obj as the type of the dynamic key, e.g. type ObjectKey = keyof typeof obj; . Use bracket notation to access the object's property, e.g. obj[myVar] .

What does it mean for an object property to be enumerable?

Enumerable properties are those properties whose internal enumerable flag is set to true, which is the default for properties created via simple assignment or via a property initializer. Properties defined via Object.

How can you get the list of all properties in an object?

To get all own properties of an object in JavaScript, you can use the Object. getOwnPropertyNames() method. This method returns an array containing all the names of the enumerable and non-enumerable own properties found directly on the object passed in as an argument.


1 Answers

Since getOwnPropertyNames can get you non-enumerable properties, you can use that and combine it with walking up the prototype chain.

function getAllProperties(obj){     var allProps = []       , curr = obj     do{         var props = Object.getOwnPropertyNames(curr)         props.forEach(function(prop){             if (allProps.indexOf(prop) === -1)                 allProps.push(prop)         })     }while(curr = Object.getPrototypeOf(curr))     return allProps } 

I tested that on Safari 5.1 and got

> getAllProperties([1,2,3]) ["0", "1", "2", "length", "constructor", "push", "slice", "indexOf", "sort", "splice", "concat", "pop", "unshift", "shift", "join", "toString", "forEach", "reduceRight", "toLocaleString", "some", "map", "lastIndexOf", "reduce", "filter", "reverse", "every", "hasOwnProperty", "isPrototypeOf", "valueOf", "__defineGetter__", "__defineSetter__", "__lookupGetter__", "propertyIsEnumerable", "__lookupSetter__"] 

Update: Refactored the code a bit (added spaces, and curly braces, and improved the function name):

function getAllPropertyNames( obj ) {     var props = [];      do {         Object.getOwnPropertyNames( obj ).forEach(function ( prop ) {             if ( props.indexOf( prop ) === -1 ) {                 props.push( prop );             }         });     } while ( obj = Object.getPrototypeOf( obj ) );      return props; } 
like image 112
airportyh Avatar answered Sep 20 '22 15:09

airportyh