let prop ="yEaR";prop =prop.toLowerCase();constlist= { [prop]:2022,};alert(list.year); // 2022
Property value shorthand
functioncreateUser(login, id) {return { login,// the same login: login id,// the same id: id };}
References and copying
Copying objectsWhen copied, the new object will be a reference to the original object, unlike regular variables, which create independent entities when copied
constoriginal= { name:"origin", id:1,};constcopy= original;alert(copy.name); // originalert(copy.id); // 1copy.name ="new copy"; // it also changes originalalert(original.name); // new copy
Object comparisonThe comparison operators == and === work the same for objects
// Two variables refer to the same objectconstoriginal= {};constcopy= original;alert(original == copy); // true
// Two independent objectsconstoriginal= {};constotherOriginal= {};alert(original == otherOriginal); // false
Cloning objectsObject.assign(target, ...sources) - copies all enumerable own properties from one or more source objects to a target object.
constitem= { name:"monitor", color:"black",};constnewItem=Object.assign({}, item);newItem.color ="gray";alert(newItem.color); // grayalert(item.color); // black
Iterating objects
Loop for...in
constitem= { name:"Pizza", price:29,};for (key in item) {alert(`${key} - ${item[key]}`);// name - Pizza// price - 29}
Object.keys(object)
Returns an array of keys of the passed object
console.log(Object.keys(item));// [name, price]
Object.values(object)
Returns an array of values of the passed object
console.log(Object.values(item));// ["Pizza", 29]
Object.entries(object)
Returns an array of the [key, value] pairs of the passed object
Constructor functionsConstructor functions are used to easily create objects. They are normal functions, but developers have agreed that such functions are capitalised and called with new operator.
functionParrot(name) {this.name = name;this.greet=function () {alert(`Hello, my name is ${this.name}`); };}constblueParrot=newParrot("Mojo");blueParrot.greet(); // Hello, my name is Mojo
Optional chainingIs a safe way to access nested object properties, even if an intermediate property doesn’t exist
constresponse= { data:"some data",};console.log(response?.data); // some dataconsole.log(response?.message); // undefined
Flags & descriptors
Object properties can store a special configuration flags in addition to the value.\
writable – if true, the value can be changed, otherwise it’s read-only.
enumerable – if true, then listed in loops, otherwise not listed.
configurable – if true, the property can be deleted and these attributes can be modified, otherwise not.
All flags default to true
Object.getOwnPropertyDescriptor(obj, property)
Allows to query the full information about a property
Object.defineProperties(obj, {prop: descr, ...})
Allows to define many properties at once
Object.getOwnPropertyDescriptors(obj)
Get all property descriptors at once
Getters & setters
Getters and setters called as accessor properties. They are essentially functions that execute on getting and setting a value, but look like regular properties to an external code.
UsageThe get keyword is used to create the getter, for the setter - set