1
00:00:01,145 --> 00:00:02,962
Okay, so we've seen how adding a property

2
00:00:02,962 --> 00:00:04,870
to a function's prototype affects all

3
00:00:04,870 --> 00:00:07,016
objects constructed using that function,

4
00:00:07,016 --> 00:00:09,073
but let's take a closer look at what

5
00:00:09,073 --> 00:00:10,809
exactly is happening behind the scenes

6
00:00:10,809 --> 00:00:13,077
when you add a property to a prototype

7
00:00:13,077 --> 00:00:15,044
like this. Specifically, let's take a look

8
00:00:15,044 --> 00:00:17,898
at exactly where those properties live.

9
00:00:17,898 --> 00:00:19,677
Here we have our Person function and two

10
00:00:19,677 --> 00:00:23,324
people derived from that function. And

11
00:00:23,324 --> 00:00:24,329
notice that the Person function's

12
00:00:24,329 --> 00:00:26,607
prototype has an age property that is set

13
00:00:26,607 --> 00:00:28,453
to 29. That means that each of our person

14
00:00:28,453 --> 00:00:32,937
objects also have an age of 29. And we

15
00:00:32,937 --> 00:00:34,149
know that if we change the

16
00:00:34,149 --> 00:00:36,213
Person.prototype .age, that will change

17
00:00:36,213 --> 00:00:39,409
the age for all persons derived from that

18
00:00:39,409 --> 00:00:41,466
function. And we know that if I change

19
00:00:41,466 --> 00:00:44,927
Person.prototype .age, that it will change

20
00:00:44,927 --> 00:00:47,835
the age for all persons derived from this

21
00:00:47,835 --> 00:00:50,302
function. But what happens if instead of

22
00:00:50,302 --> 00:00:52,230
changing the prototype's age, I change the

23
00:00:52,230 --> 00:00:55,402
age of just one of those persons?

24
00:00:55,402 --> 00:00:58,239
Interesting, in this case only Jim's age

25
00:00:58,239 --> 00:01:01,070
was changed, so what's going on here? If

26
00:01:01,070 --> 00:01:02,204
the age property is coming from the

27
00:01:02,204 --> 00:01:04,567
prototype, which is shared, how is it that

28
00:01:04,567 --> 00:01:06,336
I can change one without changing the

29
00:01:06,336 --> 00:01:07,773
other? Well, it's because we didn't

30
00:01:07,773 --> 00:01:09,860
actually change the prototype's age

31
00:01:09,860 --> 00:01:11,988
property, what we really did here was add

32
00:01:11,988 --> 00:01:15,698
a new property to the Jim object. Notice

33
00:01:15,698 --> 00:01:18,759
that Jim still has access to both values,

34
00:01:18,759 --> 00:01:24,325
so jim.age and jim.__proto__ .age, and

35
00:01:24,325 --> 00:01:26,896
jim.age is 18, where Jim's prototype age

36
00:01:26,896 --> 00:01:29,898
is still 29. The key concept to notice

37
00:01:29,898 --> 00:01:31,797
here is that prior to setting Jim's age

38
00:01:31,797 --> 00:01:34,088
directly, the Jim object never actually

39
00:01:34,088 --> 00:01:37,625
had an age property, only its prototype

40
00:01:37,625 --> 00:01:39,581
did. But if that's the case, why is it

41
00:01:39,581 --> 00:01:42,245
that if I delete this jim.age property and

42
00:01:42,245 --> 00:01:44,978
save, we can still ask for the age

43
00:01:44,978 --> 00:01:47,548
property on Jim and get a value back? This

44
00:01:47,548 --> 00:01:49,563
is because what JavaScript is really doing

45
00:01:49,563 --> 00:01:53,047
when we ask for the property value is that

46
00:01:53,047 --> 00:01:54,656
it looks at the object to see if it has a

47
00:01:54,656 --> 00:01:56,292
value for that property name. If it

48
00:01:56,292 --> 00:01:57,708
doesn't find a property on that object,

49
00:01:57,708 --> 00:02:00,736
then it asks its prototype to see if it

50
00:02:00,736 --> 00:02:02,987
has that property. We can see this further

51
00:02:02,987 --> 00:02:06,762
by using the hasOwnProperty method. So

52
00:02:06,762 --> 00:02:12,531
let's see if Jim has a property named age.

53
00:02:12,531 --> 00:02:13,662
So the hasOwnProperty method will look at

54
00:02:13,662 --> 00:02:16,311
an object and see if it has this property

55
00:02:16,311 --> 00:02:18,972
on that object itself, and you can see

56
00:02:18,972 --> 00:02:21,394
that is false. So it does not have an age

57
00:02:21,394 --> 00:02:23,983
property, but we can ask for it and get a

58
00:02:23,983 --> 00:02:26,891
value back. That value came from the

59
00:02:26,891 --> 00:02:32,356
prototype, but if I now add a jim.age,

60
00:02:32,356 --> 00:02:33,903
then you can see that hasOwnProperty

61
00:02:33,903 --> 00:02:36,088
returns true and it returns the value from

62
00:02:36,088 --> 00:02:38,469
the Jim object. And by the way, everything

63
00:02:38,469 --> 00:02:40,075
that we're doing here with properties, you

64
00:02:40,075 --> 00:02:43,321
can also do with functions on objects or

65
00:02:43,321 --> 00:02:45,891
methods, since methods are really just

66
00:02:45,891 --> 00:02:48,148
another type of property. So if my person

67
00:02:48,148 --> 00:02:49,927
prototype had an isAdult function, it

68
00:02:49,927 --> 00:02:52,016
would behave in exactly the same way that

69
00:02:52,016 --> 00:02:54,438
the age property does with regards to

70
00:02:54,438 --> 00:02:56,349
prototypes and inheritance. Next let's

71
00:02:56,349 --> 00:03:02,000
take a look at this in another way to help understand exactly what is happening here.

