Friday, 29 September, 2017 UTC


Summary

This post is a sort of grab bag to help you explore a few very useful methods to help you manage your objects in JavaScript. Weโ€™ll explore Object.keys, Object.prototype.hasOwnProperty and the newer Object.assign.
hasOwnProperty
hasOwnProperty is a method available on object instances that allows to check if an object has a property directly on its instance. Hereโ€™s a simple example that should illustrate this very clearly:
const myObj = { clown: '๐Ÿคก', police: '๐Ÿ‘ฎ', santa: '๐ŸŽ…', farmer: '๐Ÿ‘ฉโ€๐ŸŒพ' } console.log('clown' in myObj); // true console.log('valueOf' in myObj); // true console.log(myObj.hasOwnProperty('clown')); // true console.log(myObj.hasOwnProperty('valueOf')); // false
Object.keys
The Object.keys static method returns an array with the property keys on an object:
const myObj = { clown: '๐Ÿคก', police: '๐Ÿ‘ฎ', santa: '๐ŸŽ…', farmer: '๐Ÿ‘ฉโ€๐ŸŒพ' } console.log(Object.keys(myObj)); // ["clown", "police", "santa", "farmer"]
Object.keys can be really useful in allowing to use a forโ€ฆof loop over an object:
const myObj = { clown: '๐Ÿคก', police: '๐Ÿ‘ฎ', santa: '๐ŸŽ…', farmer: '๐Ÿ‘ฉโ€๐ŸŒพ' } for (let k of Object.keys(myObj)) { console.log(`Hey ${ myObj[k] }!`); } // "Hey ๐Ÿคก!" // "Hey ๐Ÿ‘ฎ!" // "Hey ๐ŸŽ…!" // "Hey ๐Ÿ‘ฉโ€๐ŸŒพ!"
Note that in the array returned from Object.keys, the keys won't necessarily be in order.
Object.assign
ES2015 (ES6) brings us a new static method on the Object constructor: Object.assign. This new method allows to easily copy values from one object to another. Notice in the following example how we use an empty object literal and copy over the properties from myObj to create a new object (myObj3) thatโ€™s a copy of myObj:
const myObj = { clown: '๐Ÿคก', police: '๐Ÿ‘ฎ', santa: '๐ŸŽ…', farmer: '๐Ÿ‘ฉโ€๐ŸŒพ' } const myObj2 = myObj; const myObj3 = Object.assign({}, myObj); console.log(Object.is(myObj, myObj2)); // true console.log(Object.is(myObj, myObj3)); // false console.log(myObj3); // Object { // clown: "๐Ÿคก", // farmer: "๐Ÿ‘ฉโ€๐ŸŒพ", // police: "๐Ÿ‘ฎ", // santa: "๐ŸŽ…" // }
In case you're wondering, Object.is is a method used to check if two objects are the same.
Note that only an object's enumerable properties will be copied over with Object.assign.
The first argument is the source object, and the subsequent arguments are source objects. You can pass-in multiple source objects, and duplicate properties in sources passed last will win:
const myObj = { clown: '๐Ÿคก', police: '๐Ÿ‘ฎ', santa: '๐ŸŽ…', farmer: '๐Ÿ‘ฉโ€๐ŸŒพ' } const myObj2 = Object.assign({}, myObj, { santa: '๐ŸŽ„', teacher: '๐Ÿ‘ฉโ€๐Ÿซ' }); console.log(myObj2); // Object { // clown: "๐Ÿคก", // farmer: "๐Ÿ‘ฉโ€๐ŸŒพ", // police: "๐Ÿ‘ฎ", // santa: "๐ŸŽ„", // teacher: "๐Ÿ‘ฉโ€๐Ÿซ" // }
Today with the likes of Redux for state management, Object.assign becomes really useful to create completely new objects from existing ones, allowing you to copy and expand objects in an immutable manner.
Bonus: Object.freeze
Use Object.freeze to shallowly freeze an object to prevent its properties from being changed. Note in this following example how, after using Object.free on an object, we canโ€™t change a property, add a new one or delete one:
const myObj = { clown: '๐Ÿคก', police: '๐Ÿ‘ฎ', santa: '๐ŸŽ…', farmer: '๐Ÿ‘ฉโ€๐ŸŒพ' } myObj.clown = 'scary'; myObj.astronaut = '๐Ÿ‘จโ€๐Ÿš€'; Object.freeze(myObj); myObj.clown = 'really scary'; myObj.student = '๐Ÿ‘ฉโ€๐ŸŽ“'; delete myObj.santa; console.log(myObj); // Object { // clown: "scary", // farmer: "๐Ÿ‘ฉโ€๐ŸŒพ", // police: "๐Ÿ‘ฎ", // santa: "๐ŸŽ…", // astronaut: "๐Ÿ‘จโ€๐Ÿš€" // }
Thereโ€™s also another useful method, Object.isFrozen, to know if an object has been frozen:
const myObj = { clown: '๐Ÿคก', police: '๐Ÿ‘ฎ', santa: '๐ŸŽ…', farmer: '๐Ÿ‘ฉโ€๐ŸŒพ' } console.log(Object.isFrozen(myObj)); // false Object.freeze(myObj); console.log(Object.isFrozen(myObj)); // true

Note that nested objects wonโ€™t automatically be frozen by Object.freeze. In the following example, the nested animals object can still have its properties changed or deleted even after the containing object has been frozen:
const myObj = { clown: '๐Ÿคก', police: '๐Ÿ‘ฎ', santa: '๐ŸŽ…', farmer: '๐Ÿ‘ฉโ€๐ŸŒพ', animals: { cow: '๐Ÿ„', rabbit: '๐Ÿ‡' } } Object.freeze(myObj); delete myObj.animals.rabbit; myObj.animals.cow = 'moo!'; console.log(myObj); // Object { // clown: "๐Ÿคก", // farmer: "๐Ÿ‘ฉโ€๐ŸŒพ", // police: "๐Ÿ‘ฎ", // santa: "๐ŸŽ…", // animals: { // cow: 'moo!' // } // }
In order to deep-freeze an object, we would have to instead recursively freeze any object property that happens to also be an object. Hereโ€™s a good utility to make deep freeze a breeze.
๐Ÿ‘จโ€๐Ÿ”ฌ P.S.: You may also be interested in learning about the new Object.values & Object.entries methods.