1
00:00:00,900 --> 00:00:05,790
Welcome back in the end of the last video we saw that when we called our constructor function we did

2
00:00:05,790 --> 00:00:07,200
not return anything.

3
00:00:07,230 --> 00:00:10,770
The entire purpose of a constructor function is to construct objects.

4
00:00:10,770 --> 00:00:14,410
So when we call the function we'd really like to have a new object be created.

5
00:00:14,640 --> 00:00:16,760
Unfortunately that's not happening right now.

6
00:00:16,950 --> 00:00:23,700
But the good news is we can fix our problems by using the new keyword when we use the new keyword.

7
00:00:23,700 --> 00:00:25,260
Everything magically works.

8
00:00:25,290 --> 00:00:30,680
The first house variable is now an object with the bedrooms bathrooms and number of square feet property

9
00:00:30,690 --> 00:00:31,070
.

10
00:00:31,560 --> 00:00:34,680
But let's see just exactly what the new keyword does.

11
00:00:34,740 --> 00:00:39,500
Just like the key word this understanding with the new keyword does can be quite tricky.

12
00:00:39,570 --> 00:00:40,750
But review the slides.

13
00:00:40,830 --> 00:00:44,960
Remember these rules and most importantly try coding a couple of examples.

14
00:00:45,060 --> 00:00:48,050
It will really help your attention of the material.

15
00:00:48,150 --> 00:00:50,720
We see that the new keyword is being used with a function.

16
00:00:50,940 --> 00:00:55,370
In fact the new keyword must be used with a function or else we'll get a type error.

17
00:00:55,530 --> 00:00:56,880
Let's see why that's the case.

18
00:00:56,890 --> 00:01:00,420
Examining just what the new keyword does first.

19
00:01:00,510 --> 00:01:02,910
It creates an object out of thin air.

20
00:01:02,910 --> 00:01:08,740
Second it sets the value of the keyword this in the constructor function which is being used with.

21
00:01:08,820 --> 00:01:11,530
To be that the object that was just created.

22
00:01:11,880 --> 00:01:14,740
Third it adds an implicit return.

23
00:01:14,760 --> 00:01:20,070
This at the end of the function so that the object created using the new keyword can be returned from

24
00:01:20,070 --> 00:01:21,490
the function.

25
00:01:21,570 --> 00:01:24,270
Finally and this one is the most tricky.

26
00:01:24,270 --> 00:01:31,140
It adds a property on the empty object which can be accessed using double underscore Prato double underscore

27
00:01:31,590 --> 00:01:34,360
this double underscore is commonly called Dunder.

28
00:01:34,380 --> 00:01:41,610
So this property is also known as Dunder Prato the Dunder Prato property links the object that was just

29
00:01:41,610 --> 00:01:45,870
created to the prototype property on the constructor function.

30
00:01:46,410 --> 00:01:50,520
That last sentence may have seemed like a blur but don't worry too much for now we're going to cover

31
00:01:50,520 --> 00:01:53,110
this in painstaking detail.

32
00:01:53,130 --> 00:01:56,930
For now let's practice by making a constructor function for a dog.

33
00:01:57,120 --> 00:02:03,570
Each dog should have a name and an age as a bonus and a function for each dog object created from the

34
00:02:03,570 --> 00:02:05,560
function called Bark.

35
00:02:05,580 --> 00:02:12,090
This function should cancelled out log the name of the dog added to the string just barked pause the

36
00:02:12,090 --> 00:02:14,400
video and try to figure this out.

37
00:02:15,120 --> 00:02:16,290
So what did you come up with.

38
00:02:16,290 --> 00:02:18,550
Hopefully it looks something like this.

39
00:02:18,560 --> 00:02:21,240
Here we have a constructor function for a dog.

40
00:02:21,240 --> 00:02:23,290
This function takes in two parameters.

41
00:02:23,340 --> 00:02:24,840
Name and age.

42
00:02:24,840 --> 00:02:27,220
Remember that you can mean your parameters anything.

43
00:02:27,420 --> 00:02:32,760
But since we want to attach properties onto the object that will create called name and age we should

44
00:02:32,760 --> 00:02:34,080
probably name our parameters.

45
00:02:34,080 --> 00:02:40,500
The same thing inside of this function where setting properties on the keyword this equal to the value

46
00:02:40,500 --> 00:02:46,710
of what is passed to the function as well as adding a function called Barch on to the keyword this which

47
00:02:46,710 --> 00:02:53,430
canceled out logs the first name property concatenated with the string just parked on the next lines

48
00:02:53,430 --> 00:02:53,470
.

49
00:02:53,490 --> 00:02:59,070
We're creating two variables rusty and Feydeau and setting them equal to the value of the dog constructor

50
00:02:59,070 --> 00:03:02,620
function being invoked with the new keyword.

51
00:03:02,630 --> 00:03:08,160
Now before we recap with a new keyword does pause the video and see if you remember any of those four

52
00:03:08,160 --> 00:03:10,100
things.

53
00:03:10,200 --> 00:03:13,950
First it creates an empty object out of thin air.

54
00:03:13,950 --> 00:03:16,740
Second it then sets the value of the keyword.

55
00:03:16,740 --> 00:03:23,460
This in the function which is being used with to be the empty object that was just created.

56
00:03:23,490 --> 00:03:25,940
Third it adds an implicit return.

57
00:03:25,950 --> 00:03:31,620
This at the end of the function so that the object created using the new keyword can be returned from

58
00:03:31,620 --> 00:03:33,150
the function.

59
00:03:33,160 --> 00:03:38,940
Fourth it adds the Dunder Prato property onto the object that was just created.

60
00:03:39,450 --> 00:03:45,080
In this video we examine how to use the keyword new to create objects from constructor functions.

61
00:03:45,120 --> 00:03:50,220
We learned about the four things that the new keyword does which I highly encourage you to review because

62
00:03:50,220 --> 00:03:56,220
we'll be covering this in much more depth and knowing the fundamentals is essential in the next video

63
00:03:56,230 --> 00:03:56,260
.

64
00:03:56,310 --> 00:04:00,870
We're going to talk about using more than one constructor function and how we can refactor our code

65
00:04:01,050 --> 00:04:02,510
to make it even cleaner.

66
00:04:02,560 --> 00:04:02,990
See that
