WEBVTT 00:00:07.190 --> 00:00:12.050 In this video we're going to learn how to create objects using constructors. 00:00:12.450 --> 00:00:18.960 So we're going to create a new file called objects using 00:00:23.940 --> 00:00:27.750 objects constructors 00:00:30.860 --> 00:00:40.690 less impose the code from the previous file and change the title to object constructors 00:00:41.930 --> 00:00:46.620 . 00:00:49.860 --> 00:00:50.520 OK so 00:00:53.370 --> 00:01:00.390 we cannot create a function as we're going to use later on to create objects. 00:01:00.660 --> 00:01:06.720 So this function is going to take a few parameters and then these problem which is going to be are going 00:01:06.720 --> 00:01:13.890 to be used to set the values of the properties of our objects. 00:01:13.890 --> 00:01:22.560 OK so to define the function I'm going to use the function keyword for a boy the name of the constructor 00:01:23.760 --> 00:01:24.990 is go. 00:01:25.050 --> 00:01:26.040 Use phone. 00:01:27.000 --> 00:01:32.520 And then we're going to need promises of the function. 00:01:32.940 --> 00:01:42.280 So if you go for instance for make model one and see color. 00:01:42.600 --> 00:01:51.000 So these are going to be values that we will insert in the parameters of the function when we invoke 00:01:51.000 --> 00:01:53.480 the function to create a new object. 00:01:54.080 --> 00:02:02.110 OK so we will see this in detail in a little bit. 00:02:05.910 --> 00:02:18.660 OK so now we get to sets the properties of the objects thus we are creating using the function using 00:02:21.300 --> 00:02:27.580 these these properties so we get to use the javascript keyword this. 00:02:27.630 --> 00:02:32.430 So basically this refers to the objects in question. 00:02:32.960 --> 00:02:33.200 OK 00:02:38.070 --> 00:02:46.320 so the properties make of these objects is going to be set to the value you make that's going to be 00:02:46.980 --> 00:02:53.120 inserting in the performances of the function which is make. 00:02:53.440 --> 00:03:01.650 OK so this is the property and this is the value of the property. 00:03:01.650 --> 00:03:02.880 All right. 00:03:03.860 --> 00:03:08.860 OK so you do the same thing for the other properties. 00:03:08.970 --> 00:03:16.680 So to set the model to model and the warranty 00:03:21.030 --> 00:03:22.110 to warranty 00:03:24.990 --> 00:03:27.300 and color 00:03:30.330 --> 00:03:32.230 to color. 00:03:33.180 --> 00:03:41.450 OK so now let's create a new object by invoking this function. 00:03:41.670 --> 00:03:42.060 So 00:03:46.290 --> 00:03:50.390 this creates an object called my phone. 00:03:51.600 --> 00:04:02.700 And now we are going to need the new jobs gives keyword forward boy the function name and the parameters 00:04:02.850 --> 00:04:08.380 that we want to use for the properties of the new objects. 00:04:08.920 --> 00:04:09.870 OK. 00:04:09.930 --> 00:04:16.380 So for instance let's use Apple for the make. 00:04:17.300 --> 00:04:23.910 And then iPhone 5 as well for the 00:04:28.500 --> 00:04:31.020 mobile and then 00:04:39.060 --> 00:04:47.940 lets use 12 for the warranty and whites are the color 00:04:55.650 --> 00:04:56.760 space the 00:05:00.590 --> 00:05:03.480 Gateless refresh the life review and to see what is going on 00:05:07.340 --> 00:05:09.180 at 12. 00:05:09.780 --> 00:05:13.920 So we just got to close the double quotes here. 00:05:14.710 --> 00:05:18.640 You know if we refresh it should be working. 00:05:18.900 --> 00:05:27.730 So let's display my phone in the console and see what we get. 00:05:32.660 --> 00:05:34.280 OK so we can see here. 00:05:34.550 --> 00:05:42.890 The object is not identified by objects anymore but it's identified by the constructor that we use to 00:05:43.340 --> 00:05:45.900 create the objects which is found. 00:05:46.250 --> 00:05:52.670 And then we can see all the properties of our objects and their corresponding values. 00:05:53.230 --> 00:05:54.390 OK. 00:05:54.590 --> 00:06:03.520 Now we're going to try to add a function inside the constructor as a method. 00:06:03.560 --> 00:06:11.010 So basically inside an object we can have both method objects properties and methods. 00:06:11.070 --> 00:06:15.270 So property properties are similar to these ones. 00:06:15.620 --> 00:06:16.150 OK. 00:06:16.330 --> 00:06:19.950 There are sets values for the properties of the objects. 00:06:20.030 --> 00:06:22.360 Now if we want to add a function. 00:06:22.820 --> 00:06:26.290 So it's called a method of the object. 00:06:26.300 --> 00:06:31.400 So for instance we can either function that we can call extended warranty 00:06:33.810 --> 00:06:44.120 extend warranty and basically the way to define the function is by using the syntax. 00:06:44.180 --> 00:06:46.220 So similar to the properties. 00:06:46.280 --> 00:06:57.800 So it's this dots the name of the function and then we use the standard definition of the function 00:07:00.860 --> 00:07:05.970 function followed by parenthesis with the parameters of the function. 00:07:06.180 --> 00:07:12.770 Then curly brackets where we put the statesman's to execute when we invoke the function. 00:07:13.100 --> 00:07:20.720 So in our case we wants to give the function a number and we want to increase the warranty of the phone 00:07:22.130 --> 00:07:23.740 by that number of months. 00:07:23.750 --> 00:07:26.500 So it's going to be this one ency 00:07:29.360 --> 00:07:29.840 and 00:07:33.770 --> 00:07:47.180 we want to add X to the warranty so we can set this warranty to this warranty plus x. 00:07:47.800 --> 00:07:49.320 OK. 00:07:50.750 --> 00:07:55.230 So let's try this function and see how it works. 00:07:55.230 --> 00:08:02.030 So to invoke the function we're going to need to sort through the name of the objects to use my phone 00:08:03.020 --> 00:08:10.750 and then we can invoke the function and give it a parameter. 00:08:11.150 --> 00:08:16.140 So let's extend the want and see by another 12 months for instance. 00:08:16.200 --> 00:08:16.690 OK. 00:08:16.940 --> 00:08:23.240 Now let's check the new warranty of the phone. 00:08:23.330 --> 00:08:27.810 So it's going to be my phone and 00:08:28.030 --> 00:08:33.330 . 00:08:34.040 --> 00:08:37.710 So now we can see that the new currency is 24 months. 00:08:37.760 --> 00:08:42.190 We extended that by 12 months from the original one. 00:08:42.200 --> 00:08:58.220 Another way of adding extra The original currency is just by saying take this warranty and odd something 00:08:58.640 --> 00:09:06.630 that equals that is equal to x and it will get exactly the same result. 00:09:07.100 --> 00:09:16.120 So this syntax is easier to use and it means something that is equal to this value. 00:09:16.700 --> 00:09:16.940 OK 00:09:31.570 --> 00:09:32.670 OK. 00:09:33.140 --> 00:09:36.110 Now we're going to try something else. 00:09:36.470 --> 00:09:42.520 We want to add a new property to my phone only for the objects. 00:09:43.070 --> 00:09:51.410 So to do that we can just use the name of the objects my phone that's the name of the new property. 00:09:51.680 --> 00:09:52.040 OK. 00:09:52.040 --> 00:10:01.470 So let's add a condition P.A. system for this my phone too like new for instance. 00:10:02.780 --> 00:10:05.390 OK. 00:10:05.390 --> 00:10:16.390 Now let's see what we get if we display the objects in the console. 00:10:24.510 --> 00:10:26.730 Well. 00:10:27.310 --> 00:10:31.120 So basically it's still showing the same structure. 00:10:31.120 --> 00:10:45.670 We don't see this new property but if we display only the property of condition than lost property it 00:10:45.670 --> 00:10:50.250 is going to be displayed will be OK. 00:10:50.290 --> 00:11:00.270 Now let's create another object using the same constructor and call this one for instance. 00:11:00.630 --> 00:11:10.190 My brother's phone and let's set it to you phone. 00:11:10.680 --> 00:11:18.170 Let's OK first let's see what happens if we don't use any vollies. 00:11:18.320 --> 00:11:19.240 It's a display 00:11:23.720 --> 00:11:24.770 this new phone. 00:11:24.900 --> 00:11:28.680 So Windows I mean do console look 00:11:34.190 --> 00:11:40.190 OK so we're not getting any errors but we can see that all the properties are defined. 00:11:40.340 --> 00:11:40.880 OK. 00:11:41.110 --> 00:11:43.720 So let's use some values. 00:11:44.720 --> 00:11:47.730 OK let's go for Apple 00:11:50.840 --> 00:11:57.130 iPhone 4 reasons. 00:11:57.870 --> 00:12:04.710 And once you have six months and we want a color black. 00:12:06.100 --> 00:12:09.420 OK so now we can see the the properties we have. 00:12:09.430 --> 00:12:13.090 Also the function of the end. 00:12:13.090 --> 00:12:18.790 So we only have the name of the function and it doesn't have any value there it just says it has a function 00:12:18.800 --> 00:12:18.960 . 00:12:19.140 --> 00:12:19.930 OK. 00:12:20.320 --> 00:12:22.860 Similar to the previous object. 00:12:23.090 --> 00:12:23.320 OK 00:12:26.470 --> 00:12:36.910 so now let's see what happens if we want to display the condition of this new object. 00:12:36.900 --> 00:12:44.780 OK so remember that we added this new property to my phone objects. 00:12:45.250 --> 00:12:53.950 OK so now let's see what happens if you want to display this new property condition for my brother's 00:12:53.950 --> 00:12:54.220 phone 00:12:54.230 --> 00:13:01.220 . 00:13:08.200 --> 00:13:08.770 OK. 00:13:08.770 --> 00:13:10.590 So it is undefined. 00:13:11.110 --> 00:13:15.490 OK so now we're going to mention something that we call a prototype. 00:13:15.490 --> 00:13:22.060 Basically all the objects they will inherit all their properties from a prototype. 00:13:22.500 --> 00:13:29.110 So the prototype brought properties are going to be inclusive of all the properties that we defined 00:13:29.530 --> 00:13:32.130 for inside our constructor. 00:13:32.400 --> 00:13:40.540 So if you want to add it in your property we need to use these properties of the prototype. 00:13:40.540 --> 00:13:43.030 So a way to do that is by 00:13:45.900 --> 00:13:58.660 using the name of the constructor and then prototype and then the name of the new property those we 00:13:58.650 --> 00:14:00.340 want to add. 00:14:00.370 --> 00:14:09.100 So in our case we want the condition to be added to the prototype and we can say that too new for example 00:14:09.100 --> 00:14:10.100 . 00:14:10.180 --> 00:14:16.200 So by default the condition of objects is going to be new by default. 00:14:16.420 --> 00:14:17.290 All right. 00:14:17.700 --> 00:14:18.310 So let's 00:14:21.310 --> 00:14:22.000 check again 00:14:25.140 --> 00:14:34.390 what we get when we check this second object's condition. 00:14:34.380 --> 00:14:40.190 So you can see now that it's by default set set to new right. 00:14:40.330 --> 00:14:47.200 But the first fones condition is still like new and we can check that again 00:14:50.100 --> 00:14:53.110 . 00:14:53.720 --> 00:14:54.480 OK. 00:14:54.490 --> 00:15:01.770 Like new and the reason why is because we changed that property's value to like new earlier in the in 00:15:01.770 --> 00:15:02.980 the in the code. 00:15:02.980 --> 00:15:09.810 So OK so this is it's regarding using constructors. 00:15:10.000 --> 00:15:16.170 Bear in mind we need to use a function give us a name. 00:15:16.170 --> 00:15:18.530 So this is what we call a constructor. 00:15:18.970 --> 00:15:20.960 So it needs barometers. 00:15:21.280 --> 00:15:23.850 You can define a function with those parameters as well. 00:15:24.310 --> 00:15:25.000 OK. 00:15:25.240 --> 00:15:34.750 So you can define properties and methods as well to invoke the function to create a new object. 00:15:34.780 --> 00:15:36.080 We need the syntax. 00:15:36.290 --> 00:15:44.910 OK so the name of the new objects upset the VAR keyword and then we need to use the new Joh's gives 00:15:44.910 --> 00:15:50.350 keyword followed by the name of the constructor and all the promises of the. 00:15:50.360 --> 00:15:55.040 And if we don't put all the parameters then some properties are going to be undefined. 00:15:55.330 --> 00:15:55.790 OK. 00:15:56.050 --> 00:16:05.530 So to add a new property to a and an object we can do that by using this syntax naming the objects thoughts 00:16:05.910 --> 00:16:07.490 name of the new property. 00:16:07.500 --> 00:16:14.980 But if you want to add a new property to the prototype so that it is generalized to all the objects 00:16:14.980 --> 00:16:23.110 created by the constructor then we need to use the constructors name dot prototype just the name of 00:16:23.110 --> 00:16:28.500 the new property and then we can set that to a value as we choose. 00:16:28.510 --> 00:16:33.240 All right.