Welcome back in the previous video we saw that when the new keyword is used a property is added to the
object created from the constructor function called donder produ.
This property links to the prototype property on the constructor function.
But what value does this prototype property have the prototype property is an object which can have
methods and properties placed on it.
These methods and properties are shared and accessible by any object that is created from that constructor
function.
When the new keyword is used in this example where adding a property on the prototype called is instructor
and setting the value to be true.
Now all of our objects that have been created from this constructor using the new keyword have access
to the is instr. property.
But how in the world did that happen.
We added a property on the person up prototype object and all of a sudden two seemingly non-related
objects have access to it.
The answer lies in under protocol.
Since these objects have a link to person dot prototype they can't access anything inside of it.
In fact this is actually the exact way that javascript finds methods and properties on objects.
And what we've just described is something called the prototype chain.
Let's talk a bit more about that.
We just saw that when we added a property to the person prototype object it was accessible from objects
created by that constructor function using the new keyword.
We then said that the reason why this was possible is because of the way that javascript looks for methods
and properties.
Let's show an example.
I'm going to make a variable called R and set it equal to an empty array.
What I actually did was shorthand for writing new array.
I use the built in constructor to javascript called array and meet a new object from it.
We also know in javascript that our arrays have a method called Push which adds something to the end
of an array.
But where is this push method defined and how does javascript know where to find it.
The answer once again is in Dunder Prato.
Let's cancel that.
D'oh.
The R variable and we see that all this object has a property called lenth But where does the push method
then come from.
Well let's examine what the Dunder Prato of this our variable is.
It is in fact the array dot prototype we can prove that by showing that are under Prato triple equals
a radar prototype evaluates to true.
So the way that javascript finds methods and properties is by looking at the object and if it can't
find the method or property you're looking for it goes to that objects donder proto.
This actually keeps happening until the property or method is found.
And if it is not found the expression evaluates to undefined we can see this even further with another
example in javascript.
Every object has a method called has own property which returns true if the object has a property specified
as a parameter of the has on property method.
What does that mean.
Sometimes a code example is worth a thousand words.
Let's go back to our variable.
We're going to try to see if that our variable has an own property of length but where is this method
located.
Let's look at our our variable with console dot DIR.
You can also just type Diyar but it doesn't seem to be here.
So what does javascript do.
It goes up the prototype chain and finds the next under produ we can see that the next under Prato is
the object prototype.
And here is where that method is found.
We can illustrate this as well with the diagram.
Once again circles are functions and squares are objects.
Here we see an R variable that has a donder Prato of a radar prototype.
The array up prototype is an object as well and has its own donder Prato that points to object prototype
every single object in javascript contains the object of prototype.
That's where methods like has own property come from the Dunder Prato of object that prototype is null
and this is where the prototype chain stops.
To recap we analyze how javascript finds methods and properties using the prototype chain we saw how
javascript will look at an object and see if the method or property you're looking for exists.
And if not it will go to that objects Dondre Prato and repeat until there is not another Dondre program
in the next video.
We'll talk more about the value of the prototype property and practice with an exercise.
See us in