Retrieving properties from an object
suggest changeCharacteristics of properties :
Properties that can be retrieved from an object could have the following characteristics,
- Enumerable
- Non - Enumerable
- own
While creating the properties using Object.defineProperty(ies), we could set its characteristics except “own”. Properties which are available in the direct level not in the prototype level (__proto__) of an object are called as own properties.
Assignment and object literals create enumerable properties. Object.defineProperty defaults enumerable to false when that attribute is omitted. “Own” is not a descriptor flag. An own property is one on the object itself, not one inherited through the prototype chain.
Purpose of enumerability :
The main purpose of setting enumerable characteristics to a property is to make the particular property’s availability when retrieving it from its object, by using different programmatic methods. Those different methods will be discussed deeply below.
Methods of retrieving properties :
Properties from an object could be retrieved by the following methods,
-
for..inloop
This loop is very useful in retrieving enumerable properties from an object. Additionally this loop will retrieve enumerable own properties as well as it will do the same retrieval by traversing through the prototype chain until it sees the prototype as null.// Ex 1 : Simple data var x = { a : 10 , b : 3} , props = []; for (const prop in x) { props.push(prop); } console.log(props); //["a","b"] //Ex 2 : Data with enumerable properties in prototype chain var x = { a : 10 , __proto__ : { b : 10 }} , props = []; for (const prop in x) { props.push(prop); } console.log(props); //["a","b"] //Ex 3 : Data with non enumerable properties var x = { a : 10 } , props = []; Object.defineProperty(x, "b", {value : 5, enumerable : false}); for (const prop in x) { props.push(prop); } console.log(props); //["a"] -
Object.keys()function
This function is part of ECMAScript 5. It returns the object’s own enumerable string keys. It does not walk the prototype chain. Symbol keys are omitted.// Ex 1 : Simple data var x = { a : 10 , b : 3} , props; props = Object.keys(x); console.log(props); //["a","b"] //Ex 2 : Data with enumerable properties in prototype chain var x = { a : 10 , __proto__ : { b : 10 }} , props; props = Object.keys(x); console.log(props); //["a"] //Ex 3 : Data with non enumerable properties var x = { a : 10 } , props; Object.defineProperty(x, "b", {value : 5, enumerable : false}); props = Object.keys(x); console.log(props); //["a"] -
Object.getOwnPropertyNames()functionThis returns every own string key, including non-enumerable ones. It still omits symbols.
Reflect.ownKeysincludes string keys and symbol keys.//Ex 1 : Simple data var x = { a : 10 , b : 3} , props; props = Object.getOwnPropertyNames(x); console.log(props); //["a","b"] //Ex 2 : Data with enumerable properties in prototype chain var x = { a : 10 , __proto__ : { b : 10 }} , props; props = Object.getOwnPropertyNames(x); console.log(props); //["a"] //Ex 3 : Data with non enumerable properties var x = { a : 10 } , props; Object.defineProperty(x, "b", {value : 5, enumerable : false}); props = Object.getOwnPropertyNames(x); console.log(props); //["a", "b"]
Miscellaneous :
A technique for retrieving all (own, enumerable, non enumerable, all prototype level) properties from an object is given below,
function getAllProperties(obj, props = []){
return obj == null ? props :
getAllProperties(Object.getPrototypeOf(obj),
props.concat(Object.getOwnPropertyNames(obj)));
}
const x = {a:10, __proto__ : { b : 5, c : 15 }};
//adding a non enumerable property to first level prototype
Object.defineProperty(x.__proto__, "d", {value : 20, enumerable : false});
console.log(getAllProperties(x));
// ["a", "b", "c", "d", ...names from Object.prototype and further]
The default parameter is ES2015. Object.getOwnPropertyNames is ES5. The walk includes inherited names, so the result is longer than the object’s own keys.