1
00:00:00,930 --> 00:00:06,480
Welcome back in the previous video we saw that javascript finds methods and properties on an object

2
00:00:06,570 --> 00:00:08,470
by working up the prototype chain.

3
00:00:08,760 --> 00:00:14,070
We also saw that the prototype property is a great place to put properties and methods that we want

4
00:00:14,070 --> 00:00:18,310
to share amongst all objects created from that constructor function.

5
00:00:18,960 --> 00:00:23,910
Let's take a look at an example and see why placing methods and properties on the prototype object is

6
00:00:23,910 --> 00:00:25,340
very efficient.

7
00:00:25,440 --> 00:00:31,050
In this example we're attaching a method called Say hi onto the object created from the person constructor

8
00:00:31,050 --> 00:00:31,840
function.

9
00:00:32,220 --> 00:00:33,650
Now this will work totally fine.

10
00:00:33,840 --> 00:00:36,430
But let's think a bit more about what we just did.

11
00:00:36,690 --> 00:00:42,800
Every time that a person object is created we have to define this function on that object.

12
00:00:42,870 --> 00:00:47,750
So when we make one million objects from the constructor we're adding the same high property.

13
00:00:47,790 --> 00:00:51,110
One million times that doesn't seem very efficient.

14
00:00:51,240 --> 00:00:56,760
It would be nice if we could just define it once and have it accessible from every object created from

15
00:00:56,760 --> 00:01:02,640
the person constructor and that is exactly what placing methods on the prototype property lets us do

16
00:01:03,630 --> 00:01:04,810
in the example below.

17
00:01:04,890 --> 00:01:10,260
We've refactored our code from above so that we only define the say high function once this code is

18
00:01:10,260 --> 00:01:16,320
much more efficient and makes use of best practices with object oriented programming in javascript.

19
00:01:16,320 --> 00:01:17,910
Now it's your turn.

20
00:01:17,970 --> 00:01:20,450
Create a constructor function for a vehicle.

21
00:01:20,730 --> 00:01:26,400
Every object created from this constructor function should have a make model and year property each

22
00:01:26,460 --> 00:01:31,660
object should also have a property called is running which should be equal to false.

23
00:01:32,070 --> 00:01:37,590
Every object created from the vehicle constructor should have a function called Turn on which changes

24
00:01:37,590 --> 00:01:43,800
the is running property for that object to true every object created from a local constructor should

25
00:01:43,860 --> 00:01:49,250
also have a function called Turn off which changes the is running property to False.

26
00:01:49,290 --> 00:01:55,050
Finally every object created from the vehicle constructor should have a method called honk which returns

27
00:01:55,050 --> 00:01:56,370
the string beep.

28
00:01:56,370 --> 00:02:01,790
Only if the is running property on that object is true.

29
00:02:01,830 --> 00:02:06,630
Think about where each of these properties and methods should be define for the vehicle objects created

30
00:02:06,630 --> 00:02:10,530
from the constructor function and will go over a solution in the next video.

31
00:02:10,530 --> 00:02:10,920
Good luck
