Compute property name ("x" + (21 * 2)), set it to true
const obj = {
["x" + (21 * 2)]: true // x42
};Symbols As Property Names
myPropSymbol = Symbol("description");
anotherObj = {
[myPropSymbol]: "Hello, symbol!",
};How to perform deep copy of an object
myObjCopy = structuredClone(myObj);
How to delete property
delete anotherObj.counter;
How to determine if property is in the object (all methods and how they differ)
"favoriteNumber" in myObj;
- The in operator will check not only the target object specified, but if not found there, it will also consult the object’s [[Prototype]] chain
myObj.hasOwnProperty("coolFact");
- By contrast, hasOwnProperty(..) only consults the target object. - object.hasOwnProperty(..) is defined as a built-in on Object.prototype, which by default is “inherited by” all normal objects.
Object.hasOwn(myObj, "favoriteNumber");
- hasOwn is invoked as a static helper external to the object value instead of via the object’s [[Prototype]], making it safer and more consistent in usage (ES2022)
Types of descriptors
value, which may or may not be writable.get(), set()) pair of functions.Descriptor properties and what they do
Object.keys(..), Object.entries(..), for..in loops, and the copying that occurs with the ...object spread and Object.assign(..). Most properties should be left enumerable, but you can mark certain special properties on an object as non-enumerable if they shouldn’t be iterated/copied.=) is allowed. To make a property “read only”, define it with writable: false.configurable: false is locked to its definition, and any further attempts to change it with Object.defineProperty(..) will fail. A non-configurable property can still be assigned new values (via =), as long as writable: true is still set on the property’s descriptor.How to set a data descriptor on an object
Object.defineProperty(anotherObj, "fave", {
value: 42,
enumerable: true, // will be seen in for of loop
writable: true, // can be changed
configurable: true // can always be re-defined/overwritten, false to prevent that.
});What are different levels of preventing object modifications. Describe methods and what operations they block.
Object.preventExtensions(myObj);
- new properties cannot be added,
- its prototype cannot be re-assigned.
Object.seal(object1);
- Same as above and:
- existing properties cannot be removed,
- their enumerability and configurability cannot be changed
Object.freeze(object1);
- Freezing an object prevents extensions and makes existing properties non-writable and non-configurable.
How prototype chain interitance works in JS
Missing methods are looked up in obects along along the [[Prototype]] chain
Not to confuse [[Prototype]] with a public property named prototype.
List some properties inherited from Object.prototype
constructor\_\_proto\_\_toString()valueOf()hasOwnProperty(..)isPrototypeOf(..)How to create object with specified Object.prototype
myObj = Object.create(differentObj);emptyObj = Object.create(null);
[[Prototype]] vs prototype
[[Prototype]] is an internal property of an object that links it to another object (its prototype), forming a prototype chain.prototype property, on the other hand, is a property of a constructor function and is used to define the prototype object for instances created using that constructor.How to inherit from another class, what to remember about (with class keyword).
An explicitly defined subclass constructor must call super(..) to run the inherited class’s initialization, and that must occur before the subclass constructor makes any references to this or finishes/returns.
class Teacher extends Person {
constructor(subject, grade) {
// Without `super()`, `this` would be undefined and cause error!
super(first, last, age);
}
}How to define private fields in JS class
#ID = null;
How to define static field in JS class
static origin = new Point2d(0, 0);
List all possible this invocation contexts
Implicit Context Invocationpoint.init(3, 4);
Default Context Invocationconst init = point.init;init(3, 4);
Explicit Context Invocation
// Assign point as the this contextinit.call(point, 3, 4);// or (same effect):init.apply(point, [3, 4]);
New Context Invocation
var anotherPoint = new point.init(3, 4);
Describe this Implicit Context Invocation
const point = {
init(x, y) {
this.x = x;
this.y = y;
},
};
point.init(3, 4);Describe this Default Context Invocation
point.), nor any other kind of this assignment mechanism, the default context assignment occurs.undefinedglobalThis, which in browser JS is essentially an alias to window, and in Node it’s global.const init = point.init; init(3, 4);
Describe this Explicit Context Invocation
const point = {
// Implementation
};
const init = point.init;
// Assign point as the this context
init.call(point, 3, 4);
// or (same effect):
init.apply(point, [3, 4]);Describe this New Context Invocation, list all the steps involved.
var anotherPoint = new point.init(3, 4);
In a sense, the new keyword hijacks a function and forces its behavior into a different mode than a normal invocation. Here are the 4 special steps that JS performs when a function is invoked with new:
- Create a brand new empty object, out of thin air.
- Link the [[Prototype]] of that new empty object to the function’s .prototype object.
- Invoke the function with the this context set to that new empty object.
- If the function doesn’t return its own object value explicitly (with a return .. statement), assume the function call should instead return the new object (from steps 1-3).
How to force specific this in function (all the methods)
Lexical this:const self = this;
Arrow function - The primary point of the => function being added to JS was to give us “lexical this” behavior without having to resort to const self = this.
Bind:this.clickHandler.bind(this)
Explicit
- foo.call(this, args...)
- foo.apply(this, [args])