let prop = "yEaR";
prop = prop.toLowerCase();
const list = {
[prop]: 2022,
};
alert(list.year); // 2022
Property value shorthand
function createUser(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
const original = {
name: "origin",
id: 1,
};
const copy = original;
alert(copy.name); // origin
alert(copy.id); // 1
copy.name = "new copy"; // it also changes original
alert(original.name); // new copy
Object comparisonThe comparison operators == and === work the same for objects
// Two variables refer to the same object
const original = {};
const copy = original;
alert(original == copy); // true
// Two independent objects
const original = {};
const otherOriginal = {};
alert(original == otherOriginal); // false
Cloning objectsObject.assign(target, ...sources) - copies all enumerable own properties from one or more source objects to a target 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.
function Animal(type, color) {
this.type = type;
this.color = color;
}
const kitten = new Animal("cat", "black");
alert(kitten.type); // cat
alert(kitten.color); // black
Methods in constructors
function Parrot(name) {
this.name = name;
this.greet = function () {
alert(`Hello, my name is ${this.name}`);
};
}
const blueParrot = new Parrot("Mojo");
blueParrot.greet(); // Hello, my name is Mojo
Property existance
Checking property existence
const response = {
data: "secret info",
status: 200,
};
console.log("data" in response); // true
console.log("message" in response); // false
Optional chainingIs a safe way to access nested object properties, even if an intermediate property doesn’t exist
const response = {
data: "some data",
};
console.log(response?.data); // some data
console.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