1
00:00:00,300 --> 00:00:06,060
Welcome back in the previous video we saw that when the new keyword is used a property is added to the

2
00:00:06,150 --> 00:00:10,310
object created from the constructor function called donder produ.

3
00:00:10,560 --> 00:00:14,780
This property links to the prototype property on the constructor function.

4
00:00:15,330 --> 00:00:21,120
But what value does this prototype property have the prototype property is an object which can have

5
00:00:21,120 --> 00:00:23,440
methods and properties placed on it.

6
00:00:23,520 --> 00:00:30,090
These methods and properties are shared and accessible by any object that is created from that constructor

7
00:00:30,090 --> 00:00:30,770
function.

8
00:00:30,780 --> 00:00:37,710
When the new keyword is used in this example where adding a property on the prototype called is instructor

9
00:00:38,010 --> 00:00:40,470
and setting the value to be true.

10
00:00:40,470 --> 00:00:46,080
Now all of our objects that have been created from this constructor using the new keyword have access

11
00:00:46,080 --> 00:00:47,910
to the is instr. property.

12
00:00:48,450 --> 00:00:49,920
But how in the world did that happen.

13
00:00:50,130 --> 00:00:56,310
We added a property on the person up prototype object and all of a sudden two seemingly non-related

14
00:00:56,340 --> 00:00:58,610
objects have access to it.

15
00:00:58,620 --> 00:01:01,080
The answer lies in under protocol.

16
00:01:01,380 --> 00:01:08,510
Since these objects have a link to person dot prototype they can't access anything inside of it.

17
00:01:08,520 --> 00:01:15,260
In fact this is actually the exact way that javascript finds methods and properties on objects.

18
00:01:15,450 --> 00:01:19,130
And what we've just described is something called the prototype chain.

19
00:01:19,350 --> 00:01:21,410
Let's talk a bit more about that.

20
00:01:21,510 --> 00:01:27,510
We just saw that when we added a property to the person prototype object it was accessible from objects

21
00:01:27,510 --> 00:01:31,120
created by that constructor function using the new keyword.

22
00:01:31,470 --> 00:01:37,110
We then said that the reason why this was possible is because of the way that javascript looks for methods

23
00:01:37,140 --> 00:01:38,210
and properties.

24
00:01:38,490 --> 00:01:40,110
Let's show an example.

25
00:01:40,110 --> 00:01:44,920
I'm going to make a variable called R and set it equal to an empty array.

26
00:01:44,940 --> 00:01:48,250
What I actually did was shorthand for writing new array.

27
00:01:48,510 --> 00:01:53,770
I use the built in constructor to javascript called array and meet a new object from it.

28
00:01:53,820 --> 00:01:59,400
We also know in javascript that our arrays have a method called Push which adds something to the end

29
00:01:59,430 --> 00:02:00,450
of an array.

30
00:02:00,660 --> 00:02:05,120
But where is this push method defined and how does javascript know where to find it.

31
00:02:05,130 --> 00:02:08,060
The answer once again is in Dunder Prato.

32
00:02:08,510 --> 00:02:09,250
Let's cancel that.

33
00:02:09,250 --> 00:02:09,640
D'oh.

34
00:02:09,660 --> 00:02:17,070
The R variable and we see that all this object has a property called lenth But where does the push method

35
00:02:17,070 --> 00:02:18,090
then come from.

36
00:02:18,420 --> 00:02:23,250
Well let's examine what the Dunder Prato of this our variable is.

37
00:02:23,250 --> 00:02:31,590
It is in fact the array dot prototype we can prove that by showing that are under Prato triple equals

38
00:02:31,680 --> 00:02:35,120
a radar prototype evaluates to true.

39
00:02:35,130 --> 00:02:40,320
So the way that javascript finds methods and properties is by looking at the object and if it can't

40
00:02:40,320 --> 00:02:46,020
find the method or property you're looking for it goes to that objects donder proto.

41
00:02:46,020 --> 00:02:49,810
This actually keeps happening until the property or method is found.

42
00:02:49,950 --> 00:02:56,010
And if it is not found the expression evaluates to undefined we can see this even further with another

43
00:02:56,010 --> 00:02:58,260
example in javascript.

44
00:02:58,260 --> 00:03:04,800
Every object has a method called has own property which returns true if the object has a property specified

45
00:03:05,160 --> 00:03:08,370
as a parameter of the has on property method.

46
00:03:08,430 --> 00:03:09,600
What does that mean.

47
00:03:09,630 --> 00:03:12,300
Sometimes a code example is worth a thousand words.

48
00:03:12,300 --> 00:03:15,670
Let's go back to our variable.

49
00:03:15,750 --> 00:03:22,170
We're going to try to see if that our variable has an own property of length but where is this method

50
00:03:22,170 --> 00:03:23,460
located.

51
00:03:23,460 --> 00:03:26,550
Let's look at our our variable with console dot DIR.

52
00:03:26,640 --> 00:03:30,800
You can also just type Diyar but it doesn't seem to be here.

53
00:03:31,080 --> 00:03:32,680
So what does javascript do.

54
00:03:32,700 --> 00:03:39,240
It goes up the prototype chain and finds the next under produ we can see that the next under Prato is

55
00:03:39,240 --> 00:03:41,090
the object prototype.

56
00:03:41,310 --> 00:03:43,810
And here is where that method is found.

57
00:03:44,010 --> 00:03:47,040
We can illustrate this as well with the diagram.

58
00:03:47,040 --> 00:03:51,150
Once again circles are functions and squares are objects.

59
00:03:51,150 --> 00:03:56,220
Here we see an R variable that has a donder Prato of a radar prototype.

60
00:03:56,220 --> 00:04:03,150
The array up prototype is an object as well and has its own donder Prato that points to object prototype

61
00:04:03,720 --> 00:04:08,840
every single object in javascript contains the object of prototype.

62
00:04:08,880 --> 00:04:15,360
That's where methods like has own property come from the Dunder Prato of object that prototype is null

63
00:04:15,660 --> 00:04:19,480
and this is where the prototype chain stops.

64
00:04:19,500 --> 00:04:25,170
To recap we analyze how javascript finds methods and properties using the prototype chain we saw how

65
00:04:25,170 --> 00:04:30,170
javascript will look at an object and see if the method or property you're looking for exists.

66
00:04:30,330 --> 00:04:36,190
And if not it will go to that objects Dondre Prato and repeat until there is not another Dondre program

67
00:04:37,140 --> 00:04:38,040
in the next video.

68
00:04:38,070 --> 00:04:42,710
We'll talk more about the value of the prototype property and practice with an exercise.

69
00:04:42,720 --> 00:04:43,420
See us in
