Section 5: Object Oriented JS and Prototypal Inheritance Flashcards

1
Q

What is Inheritance?

A

One object gets access to the properties and methods of another object

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

What it means that prototypal inheritance it´s like searching along the prototype chain?

A

It has to do with where we have access to a property or method among a sequence of objects that are connected via this prototype property, that we´re calling proto, and it´s hiddien from us.

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

Explain this image

A

Everytime i ask for a property or method, if JS doesn´t find it in the owner object, it will go donw the prototype chain to search for it, it has to do with where we have access to a property or method among a sequence of objects that are connected.

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

What is the prototype of an Object?

A

The base object

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

What is the Base Object in JS

A

It´s the very bottom of the prototype chain. Everything eventually leads to the base object, and this object have properties and methods on them.

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

What is the prototype of a function boject?

A

It´s the function Empty() {} Object. That´s the prototype of all functions, any function that we create it has this prototype, automatically

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

What is a function constructor?

A

A normal function that is used to construct objects. the ‘this’ variable points to a new empty object, and that object is returned from the function automatically

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

What happens when we invoke a function using the new operator?

what happens to this?

If we add methods and properties, to where are we adding it?

when JS will NOT return that empty object?

A

The operator ‘new’ creates an empty object

We know that the execution context generates for us a variable called ‘this’, in this case of using ‘new’, will change what the ‘this’ variable points to, it will poin to that new empty object

If we are going to add properties and methods in the form of this.firstname …, we are adding it to that empty object.

As long as the function created with ‘new’ doesn´t returned a value, JS will return that empty object.

If we want to add this by default, we can add it with parameters

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

Every function in JS has a prototype property?

A

YES

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

The prototype property, starts its life as what…?

A

Like an empty object

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

The prototype property on a function is the prototype of the function?

A

NO, its not the prototype the function?

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

What´s the prototype property on a funciton of any object, if we use the keyword new?

A

The prototype property on a function it´s the prototype of any objects created if you´re using the function as a function constructor

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

What happens with the prototype property when we use the new keyword on a function constructor?

Describe the process?

A

It creates an empty object

It sets all the properties we have defined in the function constructor

It sets the prototype of that empty object to the prototype property of the function that you then call

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

The prototype property of all functions is where the prototype chain points for any objects created using that function as a constructor

A

YES

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

Can we add something later to the prototype, on the fly? and why?

A

Yes, because the prototype chain just look at these objects at the moment you try to access any one of their methods and properties

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

It’s better to have the methods of function constructor, added to the prototype property instead that inside the function constructor?

and why?

A

YES, because it´s better performance. Remember Objects, takes memory space

If we have 1000 objects created from the function constructor, if we have methods on it, we will have (1000 x methods), every object with get its own copy of the methods, but adding the methods to the prototype, we can just have one method and that´s all.

Instead in the case of properties its logic that every object will have different values!

17
Q

Describe this image

A

This is the prototype chain

In dark green its the Object prototype, that all Objects delegate to them, are related to them via the prototype chain.

In light blue, it´s the arrays prototype, who its also delegate to the Object prototype

etc…

18
Q

In simple terms, what is the prototype property ? It´s a ref…..

A

It´s a reference to another object, where it goes and looks for properties and methods if not found on the original object

19
Q

The scope chain is about looking for where we have access to a variable.

The prototype chain has to do with….

A

where we have access to a property or method amongst a sequence of objects that are connected via this prototype property

20
Q

We have 2 objects

jonh = { }

person = { }

If we do this:

john.prototype = person

What are we doing?

A

If i try to access a property or method on John, and it doesn´t exist on “john”, it will go to “person” and try to find it

21
Q

What is Reflection?

A

An object can look at itself, listing and changing its properties and methods

22
Q

What is extend?

A

It’s a very usefull pattern

Consist of taking all the properties and methods from other objects, and adds them to the object that we want.

23
Q

What is the difference between using the extend pattern and prototypal inheritance?

A

When we use extend, we are essentially placing the properties and methods from one object into another object, physically placing…

Prototypal inheritance, its just searching across the prototype chain for properties and methods.

24
Q

How the concept of reflection and extend relate to each other?

A

Thanks to reflection an object can look at itself, listing all the properties and methods. So JS objects has this ability to look at itself, so we can use that to implement the pattern of extend, to copy the properties and methods of one objecto to another object.

Tx to reflection we look at properties and methods from one object, and using extend we can copy those properties and methods to other object.