Tutorial by Examples

5 Object.keys(obj) returns an array of a given object's keys. var obj = { a: "hello", b: "this is", c: "javascript!" }; var keys = Object.keys(obj); console.log(keys); // ["a", "b", "c"]
6 ES6's Object.assign() function can be used to copy all of the enumerable properties from an existing Object instance to a new one. const existing = { a: 1, b: 2, c: 3 }; const clone = Object.assign({}, existing); This includes Symbol properties in addition to String ones. Object rest/sp...
5 It allows us to define a property in an existing object using a property descriptor. var obj = { }; Object.defineProperty(obj, 'foo', { value: 'foo' }); console.log(obj.foo); Console output foo Object.defineProperty can be called with the following options: Object.definePropert...
5 Using property descriptors we can make a property read only, and any attempt to change it's value will fail silently, the value will not be changed and no error will be thrown. The writable property in a property descriptor indicates whether that property can be changed or not. var a = { }; ...
5 We can avoid a property from showing up in for (... in ...) loops The enumerable property of the property descriptor tells whether that property will be enumerated while looping through the object's properties. var obj = { }; Object.defineProperty(obj, "foo", { value: 'show', enu...
5 A property's descriptor can be locked so no changes can be made to it. It will still be possible to use the property normally, assigning and retrieving the value from it, but any attempt to redefine it will throw an exception. The configurable property of the property descriptor is used to dis...
5 Treat a property as a combination of two functions, one to get the value from it, and another one to set the value in it. The get property of the property descriptor is a function that will be called to retrieve the value from the property. The set property is also a function, it will be call...
While object property notation is usually written as myObject.property, this will only allow characters that are normally found in JavaScript variable names, which is mainly letters, numbers and underscore (_). If you need special characters, such as space, ☺, or user-provided content, this is poss...
Sometimes the property name needs to be stored into a variable. In this example, we ask the user what word needs to be looked up, and then provide the result from an object I've named dictionary. var dictionary = { lettuce: 'a veggie', banana: 'a fruit', tomato: 'it depends on who yo...
Disclaimer: Creating array-like objects is not recommend. However, it is helpful to understand how they work, especially when working with DOM. This will explain why regular array operations don't work on DOM objects returned from many DOM document functions. (i.e. querySelectorAll, form.elements)...
5 Object.freeze makes an object immutable by preventing the addition of new properties, the removal of existing properties, and the modification of the enumerability, configurability, and writability of existing properties. It also prevents the value of existing properties from being changed. Howev...
5 Object.seal prevents the addition or removal of properties from an object. Once an object has been sealed its property descriptors can't be converted to another type. Unlike Object.freeze it does allow properties to be edited. Attempts to do this operations on a sealed object will fail silently ...
6 var myIterableObject = {}; // An Iterable object must define a method located at the Symbol.iterator key: myIterableObject[Symbol.iterator] = function () { // The iterator should return an Iterator object return { // The Iterator object must implement a method, next() next: func...
7 Object spreading is just syntactic sugar for Object.assign({}, obj1, ..., objn); It is done with the ... operator: let obj = { a: 1 }; let obj2 = { ...obj, b: 2, c: 3 }; console.log(obj2); // { a: 1, b: 2, c: 3 }; As Object.assign it does shallow merging, not deep merging. let obj3 = ...
Properties are members of an object. Each named property is a pair of (name, descriptor). The name is a string that allows access (using the dot notation object.propertyName or the square brackets notation object['propertyName']). The descriptor is a record of fields defining the bevahiour of the pr...
Get the description of an specific property in an object. var sampleObject = { hello: 'world' }; Object.getOwnPropertyDescriptor(sampleObject, 'hello'); // Object {value: "world", writable: true, enumerable: true, configurable: true}
When you want a complete copy of an object (i.e. the object properties and the values inside those properties, etc...), that is called deep cloning. 5.1 If an object can be serialized to JSON, then you can create a deep clone of it with a combination of JSON.parse and JSON.stringify: var existing...
The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object. Use it to assign values to an existing object: var user = { firstName: "John" }; Object.assign(user, {last...
You can access each property that belongs to an object with this loop for (var property in object) { // always check if an object has a property if (object.hasOwnProperty(property)) { // do stuff } } You should include the additional check for hasOwnProperty because an o...
Characteristics 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...

Page 1 of 2