( O O P ) OBJECT ORIENTED PROGRAMMING. Flashcards

1
Q

What are JS OBJECTS?

A

1.) A JavaScript object is an entity that has
properties.

2.) Each property is a key/value pair.

3.) The key is the property name.

The value of a property can be a number, string, etc or a function. In this case, the property is called a method.

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

How do you create Objects?

A

An object can be created with figure brackets {…} with an optional list of properties. A property is a “key: value” pair, where key is a string (also called a “property name”), and value can be anything.

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

Adding and removing from an Object

A

We can add, remove and read files from it at any time.
1.) user.isAdmin = true; uses boolean to add

2.) delete user.age; uses delete

3.) We can also use multiword property names, but then they must be quoted
{ “likes birds”: true}

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

Property names limitations

A

A variable cannot have a name equal to one of the language-reserved words like
“for”,
“let”,
“return” etc.
But for an object property, there’s no such restriction

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

Object Magic

A

it’s possible to access any property. There will be no error if the property doesn’t exist!

Reading a non-existing property just returns undefined. So we can easily test whether the property exists

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

What is Object reference?

A

Objects are assigned and copied by reference. In other words, a variable stores not the “object value”, but a “reference” (address in memory) for the value. So copying such a variable or passing it as a function argument copies that reference, not the object itself.

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

How do you clone and merge Objects?

A

To make a “real copy” (a clone) we can use
1.) Object.assign for the so-called “shallow copy” (nested objects are copied by reference)
2.) A “deep cloning” function structuredClone or
3.)use a custom cloning implementation, such as _.cloneDeep(obj).

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

What is JS Garbage Collection?

A

The main things to know:

1.) Garbage collection is performed automatically. We cannot force or prevent it.
2.) Objects are retained in memory while they are reachable.
3.) Being referenced is not the same as being reachable (from a root): a pack of interlinked objects can become unreachable as a whole,
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do functions work in Objects?

A

1.) Functions that are stored in object properties are called “methods”.

2.) Methods allow objects to “act” like object.doSomething().

3.) Methods can reference the object as this.

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

How does “this” apply in methods?

A

1.The value of this is defined at run-time.

2.)When a function is declared, it may use this, but that this has no value until the function is called.

3.) A function can be copied between objects.

4.) When a function is called in the “method” syntax:
object.method(), the value of this during the call is object.

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

What is Optional-Chaining?

A

The optional chaining ?. syntax has three forms:

1.) obj?.prop – returns obj.prop if obj exists, otherwise undefined.

2.) obj?.[prop] – returns obj[prop] if obj exists, otherwise undefined.

3.) obj.method?.() – calls obj.method() if obj.method exists, otherwise returns undefined.

A chain of ?. allows to safely access nested properties.

Still, we should apply ?. carefully, only where it’s acceptable, according to our code logic, that the left part doesn’t exist. So that it won’t hide programming errors from us, if they occur.

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

What is a Symbols() in JS ?

A

Symbol is a primitive type for unique identifiers.

1.) Symbols are created with Symbol() call with an optional description (name).

2.) Symbols are always different values, even if they have the same name. If we want same-named symbols to be equal, then we should use the global registry: Symbol.for(key) returns (creates if needed) a global symbol with key as the name. Multiple calls of Symbol.for with the same key return exactly the same symbol.

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

What are the two main use cases for Symbols() ?

A

“Hidden” object properties.

1.) If we want to add a property into an object that “belongs” to another script or a library, we can create a symbol and use it as a property key. A symbolic property does not appear in for..in, so it won’t be accidentally processed together with other properties. Also it won’t be accessed directly, because another script does not have our symbol. So the property will be protected from accidental use or overwrite.

So we can “covertly” hide something into objects that we need, but others should not see, using symbolic properties.

2.) There are many system symbols used by JavaScript which are accessible as Symbol.*. We can use them to alter some built-in behaviors. For instance, later in the tutorial we’ll use Symbol.iterator for iterables, Symbol.toPrimitive to setup object-to-primitive conversion and so on.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Object Oriented Programming?

A

1.) Object-Oriented Programming, or OOP, is a programming paradigm that uses objects containing both data and behavior to create programs.

2.) A class is an object-oriented abstraction for an idea or a concept manipulated by a program. It offers a convenient syntax to create objects representing this concept.

3.) A JavaScript class is defined with the class keyword. It can only contain methods. The constructor() method, called during object creation, is used to initialize the object, often by giving it some data properties. Inside methods, the this keyword represents the object on which the method was called.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are OOP Prototypes?

A

1.) JavaScript’s OOP model is based on prototypes. Any JavaScript object has an internal property which is a link (a reference) to another object: its prototype. Prototypes are used to share properties and delegate behavior between objects.

2.) When trying to access a property that does not exist in an object, JavaScript tries to find this property in the prototype chain of this object by first searching its prototype, then its prototype's own prototype, and so on.

3.) There are several ways to create and link JavaScript objects through prototypes. One is to use the Object.create() method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to loop through an Object.

A

for…in

The for…in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties.