Section 5: Object Oriented JS and Prototypal Inheritance Flashcards
What is Inheritance?
One object gets access to the properties and methods of another object
What it means that prototypal inheritance it´s like searching along the prototype chain?
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.
Explain this image
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.
What is the prototype of an Object?
The base object
What is the Base Object in JS
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.
What is the prototype of a function boject?
It´s the function Empty() {} Object. That´s the prototype of all functions, any function that we create it has this prototype, automatically
What is a function constructor?
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
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?
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
Every function in JS has a prototype property?
YES
The prototype property, starts its life as what…?
Like an empty object
The prototype property on a function is the prototype of the function?
NO, its not the prototype the function?
What´s the prototype property on a funciton of any object, if we use the keyword new?
The prototype property on a function it´s the prototype of any objects created if you´re using the function as a function constructor
What happens with the prototype property when we use the new keyword on a function constructor?
Describe the process?
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
The prototype property of all functions is where the prototype chain points for any objects created using that function as a constructor
YES
Can we add something later to the prototype, on the fly? and why?
Yes, because the prototype chain just look at these objects at the moment you try to access any one of their methods and properties