Objects and Classes Flashcards

(22 cards)

1
Q

Compute property name ("x" + (21 * 2)), set it to true

A
const obj = {
  ["x" + (21 * 2)]: true // x42
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Symbols As Property Names

A
myPropSymbol = Symbol("description");

anotherObj = {
  [myPropSymbol]: "Hello, symbol!",
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to perform deep copy of an object

A

myObjCopy = structuredClone(myObj);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to delete property

A

delete anotherObj.counter;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to determine if property is in the object (all methods and how they differ)

A

"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.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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Types of descriptors

A
  • Data descriptor is a property that has a value, which may or may not be writable.
  • Accessor descriptor is a property described by a getter-setter (get(), set()) pair of functions.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Descriptor properties and what they do

A
  • The enumerable attribute controls whether the property will appear in various enumerations of object properties, such as 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.
  • The writable attribute controls whether a value assignment (via =) is allowed. To make a property “read only”, define it with writable: false.
  • The configurable attribute controls whether a property’s descriptor can be re-defined/overwritten. A property that’s 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to set a data descriptor on an object

A
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.
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are different levels of preventing object modifications. Describe methods and what operations they block.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How prototype chain interitance works in JS

A

Missing methods are looked up in obects along along the [[Prototype]] chain

Not to confuse [[Prototype]] with a public property named prototype.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

List some properties inherited from Object.prototype

A
  • constructor
    • \_\_proto\_\_
    • toString()
    • valueOf()
    • hasOwnProperty(..)
    • isPrototypeOf(..)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to create object with specified Object.prototype

A

myObj = Object.create(differentObj);
emptyObj = Object.create(null);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

[[Prototype]] vs prototype

A
  • [[Prototype]] is an internal property of an object that links it to another object (its prototype), forming a prototype chain.
  • The 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How class interitance works in JS

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to define private fields in JS class

A

~~~
class Point2d {
#ID = null;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to define static field in JS class

A

~~~
class Point2d {

static origin = new Point2d(0, 0);

}
```
17
Q

List this invocation methods:

A
  • Implicit Context Invocation
  • Default Context Invocation
  • Explicit Context Invocation
  • New Context Invocation
18
Q

Describe this Implicit Context Invocation

A

```javascript
const point = {
init(x, y) {
this.x = x;
this.y = y;
},
};

// invoke the init(..) function with this referencing point
point.init(3, 4);
~~~

19
Q

Describe this Default Context Invocation

A
  • When there’s no implicit context (point.), nor any other kind of this assignment mechanism, the default context assignment occurs.
  • Default context:
    • For strict mode: undefined
    • For non-strict mode, the default context is the global object - JS defines it as globalThis, which in browser JS is essentially an alias to window, and in Node it’s global.

```javascript
const init = point.init;
init(3, 4);
~~~

20
Q

Describe this Explicit Context Invocation

A

```javascript
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]);
~~~

21
Q

Describe this New Context Invocation, list all the steps involved.

A

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).

22
Q

How to force specific this context (all the methods)

A

Lexical this:
var context = this; // sometimes called self

Arrow function - he primary point of the => function being added to JS was to give us “lexical this” behavior without having to resort to var context = this.

Bind:
this.clickHandler.bind(this)