Objects Flashcards

1
Q

Ways to define a property in an object

A
  1. With literal notation
  2. Object.defineProperty(obj, ‘key’, {
    configurable - if set to false, the value can’t be modified (changed or deleted),
    enumerable - if set to false, the property can’t be iterated over,
    writable - if set to false value can’t be changed
    })
  3. Object.defineProperties(obj, {
    ‘key1’: {
    configurable: …, writable: …, enumerable: …
    },
    ‘key2’: {…},

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

Ways to get a certain Object’s property

A
  1. Accessing it directly: obj.key, obj[‘key’]
  2. Object.getOwnPropertyDescriptor(obj, ‘key’) - returns { value: …, enumerable: …, writable: …, configurable: … }
    3*. obj.hasOwnProperty(‘key’) - checks if an Object has a certain property
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Ways to get all* properties of the Object

A
  1. Using an iterator (does not include non-enumerable properties):
    for(let key in obj) { … }
  2. Object.keys(obj) (does not include non-enumerable properties)
  3. Object.getOwnPropertyNames(obj) - includes non-enumerable properties
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Preventing modifications of an Object

A
  1. Object.freeze(obj) - prevents from adding new properties and from deleting or changing existing properties.
    Object.isFrozen(obj) - checks if object is frozen
  2. Object.preventExtensions(obj) - prevents adding new properties to the Object
    Object.isExtensible(obj) - checks if an Object is extensible
  3. Object.seal(obj) - prevents adding new properties or deleting existing ones
    Object.isSealed(obj) - checks if Object is sealed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Calling Objects

Problem: I have a method of an Object obj1 and want call it for another Object obj2 (in context of another object)

A

Solutions:

  1. Function.call: obj1.method.call(obj2, arg1, arg2, … )
  2. Function.apply: obj1.method.apply(obj2, [arg1, arg2, … ])

The only difference is that one accepts a list of arguments and the other one expects an array of arguments

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

Ways to create an Object

A
  1. Literal notation: const obj = {}
  2. Object.create(obj or Object.prototype, { key: { value: …, writable: …, … } }) - creates an object based on the supplied prototype with given values
  3. new constructor(arg1, arg2, … ) - new operator creates an instance of an object, accepting a constructor function and a series of of optional arguments for the constructor function.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does ‘new’ operator work?

A

const obj = new constructor(args)

  1. JS creates a new object
  2. JS links the constructor of obj to the supplied constructor function
  3. JS links the obj.prototype to the constructor.prototype by reference
  4. JS assigns any supplied args to the obj
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

OOP-1: Abstraction. Decoupling

A

Abstraction is an invented construct that transforms a real-life object into a set of values and methods.
Abstractions give a mechanism for a process known as decoupling - breaking up complexities of the subject into smaller parts.
Goals of abstraction:
1. Hiding information
2. Decoupling contents into modules
3. Defining a clear interface between objects

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

OOP-2: Encapsulation

A

Goals:

  1. Hide implementation
  2. Promote modularity
  3. Protect the internal state of an object

In ES5 encapsulation can be achieved by using local variables in the constructor function

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

OOP-3: Polymorphism

A

Polymorphism describes the capability of one object to act like another in the given context.
Ad hoc polymorphism provides the ability to use the context of the call to an object unrelated to the function.

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

OOP-4: Inheritance

A

Inheritance defines semantic hierarchies between objects, by allowing children to create specializations, generalizations and variations of a parent class.
In JS inheritance is prototypal, meaning that it uses prototype chains.

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

Ways of accessing Prototype

A
  1. Given a constructor() function, constructor.prototype will define the prototype for all objects created using ‘new constructor()’
  2. Object.getPrototypeOf(obj) returns the prototype of an object
  3. obj.__proto__ is the same as previous method and should not be used, it is mentioned for the sake of completeness
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

ES6! Classes-1: defining classes

A

Classes in ES6 are a syntax sugar over the prototyping chain. They can be defined in two ways:

1. Class declaration:
class Class {
      constructor() { ... }
}
2. Class expressions that can be named or unnamed:
let Class = class ClassName* {
    constructor() { ... }
}
    • is optional
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

ES6! Classes-2: Constructor. Private and public fields

A

Constructor is a special method for creating and initializing an object created with a class. Constructor can use a super() keyword to call a constructor of a parent class.

Public and private fields can be defined before the constructor or inside constructor after super() runs. Private fields are marked with a # (i. e. this.#privateField)

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

ES6! Classes-3: Static fields

A

Static fields and methods do not belong to any instance of a class, but to the class itself. For example if we were to have a class [Class] with a static method [stat()] and there was an instance of this class [inst], then static method wouldn’t be available to the instance [inst.stat() wouldn’t work] but it can be called from the class itself [Class.stat()]

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

ES6! Classes-4: Extends keyword

A

Extends keyword is used to create a class as a child of another class, as in

class Subclass extends Superclass { … }

Where Superclass can be another class or a traditional constructor function.