1
00:00:00,180 --> 00:00:01,560
Hey, welcome back, kid.

2
00:00:01,590 --> 00:00:06,750
I know the last lesson was fairly lengthy in this lesson, I'm going to try to keep it short and I do

3
00:00:06,750 --> 00:00:07,980
apologize for the last one.

4
00:00:08,280 --> 00:00:11,700
This lesson we are going to be looking at prototype in JavaScript.

5
00:00:11,910 --> 00:00:18,120
So object prototype is a property of an object constructor and it's also the end of a prototype chain.

6
00:00:18,240 --> 00:00:22,340
So all JavaScript objects inherit properties and methods from the prototype.

7
00:00:22,530 --> 00:00:29,760
So this gives you the ability to add to a particular object and add additional functionality and methods

8
00:00:29,760 --> 00:00:30,830
to those objects.

9
00:00:30,840 --> 00:00:33,540
And of course, you have to declare it before you try to use it.

10
00:00:34,350 --> 00:00:42,270
And objects such as the DETE object so you can update and date prototype can inherit within the date

11
00:00:42,270 --> 00:00:45,330
object, so you can use existing ones as well as create your own.

12
00:00:45,690 --> 00:00:50,460
And there's an example of this and there's more information about prototype, of course, at the Mozilla

13
00:00:50,460 --> 00:00:51,320
Developer Network.

14
00:00:51,660 --> 00:00:56,370
So more information about just different ways to create prototypes, said work with prototypes.

15
00:00:56,610 --> 00:00:57,750
They've got an example here.

16
00:00:57,750 --> 00:01:04,560
So object prototype value of so this is extending on value of and adding in different functionality

17
00:01:04,560 --> 00:01:07,830
to those so you can use those and utilize them.

18
00:01:07,830 --> 00:01:13,920
So they've got an example here of the person and updating that prototype with greet.

19
00:01:14,260 --> 00:01:20,790
So now when you call to person, so using it as an object, you can extend on that person and have a

20
00:01:20,790 --> 00:01:21,880
greeting coming back.

21
00:01:22,410 --> 00:01:24,870
So let me show you a little bit more, opening up the editor.

22
00:01:25,050 --> 00:01:28,800
Let's create our first function and we can call this one just person.

23
00:01:29,070 --> 00:01:33,630
So we've got first and last as parameters that are coming into the function.

24
00:01:33,870 --> 00:01:40,860
And then we're using this first name is going to be equal to whatever the value of first is in this

25
00:01:40,860 --> 00:01:44,960
last name is going to be equal to whatever the value of last is.

26
00:01:45,130 --> 00:01:49,710
So it's going to be a quick and easy way to create an object and then we're going to extend on that

27
00:01:49,710 --> 00:01:50,830
person prototype.

28
00:01:51,060 --> 00:01:57,780
So if we create a new object and we could just call it meat and we can use new person, so extending

29
00:01:57,780 --> 00:02:04,050
on that object that we have, we can pass in the first name as well as the last name that we can use

30
00:02:04,230 --> 00:02:08,100
in the object if we consult, like about me.

31
00:02:08,430 --> 00:02:09,980
What do you think is going to happen?

32
00:02:10,020 --> 00:02:14,380
You're going to see that we've got person and we've got first name, last name so we can use that.

33
00:02:14,400 --> 00:02:20,550
So let's create a prototype on person so that a person object and using prototype, we're extending

34
00:02:20,550 --> 00:02:20,900
on that.

35
00:02:21,150 --> 00:02:24,330
So we're going to add in a method called full name and function.

36
00:02:24,540 --> 00:02:29,040
So it's going to be a method where it's going to use the values are contained within the object.

37
00:02:29,040 --> 00:02:37,830
So seeking this and it's taking this first name value and adding with space in the middle last name,

38
00:02:37,830 --> 00:02:40,120
and that's what's going to return back as the prototype.

39
00:02:40,590 --> 00:02:42,390
So now that gives me the ability.

40
00:02:42,390 --> 00:02:48,840
So if I save that and I'm simply outputting it as an object into the console, but now if I do console,

41
00:02:48,840 --> 00:02:54,090
log me full name or simply just call back to see full name.

42
00:02:54,300 --> 00:02:59,610
So I'm using the prototype and it's returning back by fullName that have passed on from the object that

43
00:02:59,610 --> 00:03:00,120
was created.

44
00:03:00,270 --> 00:03:07,200
So there is a challenge to this lesson and we did mention that the date object in JavaScript, so you

45
00:03:07,200 --> 00:03:08,660
can add a prototype to it.

46
00:03:08,670 --> 00:03:14,460
So extending on a date where you can have the date method and then you can simply add days to it.

47
00:03:14,730 --> 00:03:21,720
So the objective is to create a prototype on the date object where you can add days to it specifying

48
00:03:21,720 --> 00:03:23,160
a parameter that comes in.

49
00:03:23,250 --> 00:03:25,140
And this is the number of days that get added.

50
00:03:25,140 --> 00:03:28,650
And so go ahead and try this out and I'll walk you through the solutions.

51
00:03:28,650 --> 00:03:31,260
You can pause the video here and I'll walk you through the solution.

52
00:03:31,560 --> 00:03:33,200
This could be a really short solution.

53
00:03:33,810 --> 00:03:40,140
So taking our DETE prototype, so the date object, because that exists in JavaScript and then using

54
00:03:40,140 --> 00:03:46,760
prototype to extend on it, we're creating a method called add days and then setting that up as a function.

55
00:03:47,010 --> 00:03:51,960
So it's taking in a parameter of days and it's going to take that and make a calculation.

56
00:03:51,960 --> 00:03:58,170
So new Dete, it's going to take the current value of and then add days to it.

57
00:03:58,230 --> 00:03:59,490
We're going to multiply this.

58
00:03:59,770 --> 00:04:03,810
So this is eighty six for E five.

59
00:04:03,810 --> 00:04:10,110
So that should give us the amount of milliseconds in the days and basically allowing us to make that

60
00:04:10,110 --> 00:04:10,830
calculation.

61
00:04:11,160 --> 00:04:17,040
So now if we do console log and the nice thing about prototype is you can use it over and over again

62
00:04:17,310 --> 00:04:23,700
so we can add days to it and we can specify how many days we want to add in to the date so that when

63
00:04:23,700 --> 00:04:27,750
we refresh it, we see we automatically add seven days into the current date.

64
00:04:27,900 --> 00:04:28,860
Today is the third.

65
00:04:28,980 --> 00:04:30,450
We're coming back at the tenth.

66
00:04:30,660 --> 00:04:36,480
If I add seventeen and so we can do 20, we can do a hundred and whatever we want to do.

67
00:04:36,480 --> 00:04:37,350
So just save that.

68
00:04:37,350 --> 00:04:38,190
Refresh that.

69
00:04:38,340 --> 00:04:42,990
So that's coming into July 18th and we can even out a thousand.

70
00:04:43,260 --> 00:04:47,040
So refresh going up to 20, 22 and so on.

71
00:04:47,790 --> 00:04:52,590
So coming up next is try and catch such a very interesting one that you can utilize in JavaScript.
