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
It’s better to have the methods of function constructor, added to the prototype property instead that inside the function constructor?
and why?
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!
Describe this image

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…
In simple terms, what is the prototype property ? It´s a ref…..
It´s a reference to another object, where it goes and looks for properties and methods if not found on the original object
The scope chain is about looking for where we have access to a variable.
The prototype chain has to do with….
where we have access to a property or method amongst a sequence of objects that are connected via this prototype property
We have 2 objects
jonh = { }
person = { }
If we do this:
john.prototype = person
What are we doing?
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
What is Reflection?
An object can look at itself, listing and changing its properties and methods
What is extend?
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.
What is the difference between using the extend pattern and prototypal inheritance?
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.
How the concept of reflection and extend relate to each other?
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.