0:00:00.000,0:00:03.184 Now let's go ahead[br]and make another constructor. 0:00:03.184,0:00:06.962 So this time,[br]I'm going to create a Dalmatian class.... 0:00:09.180,0:00:12.553 And it's going to be a dog[br]with the color... 0:00:15.770,0:00:19.781 [speaking while typing][br]"white with black spots." 0:00:22.995,0:00:25.050 But obviously, I haven't... 0:00:25.050,0:00:27.660 created any kind of relationship[br]between "Dalmation" 0:00:27.660,0:00:31.631 and "dog" just yet,[br]so if I reload-- 0:00:31.631,0:00:33.540 oh! Syntax error. 0:00:36.347,0:00:39.176 Let's see where that is.... 0:00:39.176,0:00:42.060 Oh, I forgot my parentheses. 0:00:42.060,0:00:43.790 This is a function. 0:00:48.767,0:00:51.482 So obviously, if I set... 0:00:51.482,0:00:53.940 [speaking while typing][br]"spot" 0:00:53.940,0:00:55.712 to "new... 0:00:57.546,0:01:01.333 "Dalmation," Dal-ma-tion.... 0:01:04.328,0:01:06.665 Let's try that again. 0:01:06.665,0:01:09.000 So now Spot is a dog, 0:01:09.000,0:01:11.194 but only the color property[br]is set. 0:01:11.194,0:01:14.857 So I can say "spot.color,"[br]and that works fine, 0:01:14.857,0:01:17.753 but if I say "spot.speak," 0:01:18.729,0:01:20.163 that doesn't work at all. 0:01:20.163,0:01:24.030 So, in order to define[br]the inheritance relationship, 0:01:24.030,0:01:26.040 I need to do something[br]a little odd. 0:01:26.040,0:01:28.260 So let me give you[br]a little background first. 0:01:28.260,0:01:30.630 So, if you're used to a language 0:01:30.630,0:01:35.190 like C++ or Java or C#, 0:01:35.190,0:01:38.254 then the way that you define[br]an inheritance relationship 0:01:38.254,0:01:41.730 is you have a base class,[br]like dog, 0:01:41.730,0:01:44.040 and then you extend[br]that base class 0:01:44.040,0:01:47.387 by creating a subclass,[br]like Dalmation. 0:01:47.387,0:01:49.470 So... 0:01:49.470,0:01:51.750 the way you do it in JavaScript 0:01:51.750,0:01:53.010 is a little different. 0:01:53.010,0:01:56.220 So JavaScript is known[br]as a prototypal 0:01:56.220,0:01:58.530 or "proto-tip-pal" language, 0:01:58.530,0:02:01.500 where inheritance is based[br]on prototypes. 0:02:01.500,0:02:02.908 So what is a prototype? 0:02:02.908,0:02:05.372 A prototype is an object. 0:02:05.372,0:02:08.220 So the way you define[br]an inheritance relationship 0:02:08.220,0:02:12.330 is you set a base prototype,[br]a base object, 0:02:12.330,0:02:15.797 that the sub-object[br]that you're creating 0:02:15.797,0:02:18.210 inherits all the properties from. 0:02:18.210,0:02:20.703 So instead of extending a class, 0:02:20.703,0:02:23.397 which is a blueprint[br]for a set of objects, 0:02:23.397,0:02:26.640 instead,[br]you're extending an object 0:02:26.640,0:02:29.730 by adding additional methods[br]and additional properties 0:02:29.730,0:02:32.400 to the base set of objects[br]and properties 0:02:32.400,0:02:34.530 that that object has. 0:02:34.530,0:02:36.210 So what I want to do, basically, 0:02:36.210,0:02:39.000 is I want to create a new dog, 0:02:39.000,0:02:42.330 which is the prototype[br]for all Dalmations, 0:02:42.330,0:02:44.610 and then I want to inherit 0:02:44.610,0:02:47.010 that prototype 0:02:47.010,0:02:49.950 for all Dalmations that I create. 0:02:49.950,0:02:52.170 The way that works[br]is the constructor, 0:02:52.170,0:02:54.394 the Dalmation function, 0:02:54.394,0:02:58.014 has a prototype property,[br]which is an object. 0:02:58.014,0:02:59.700 So whenever you create a function, 0:02:59.700,0:03:01.470 there's going to be[br]a prototype property 0:03:01.470,0:03:03.960 for that function;[br]let's take a look at it. 0:03:03.960,0:03:06.546 So Dalmation, 0:03:06.546,0:03:10.728 which is a function.prototype, 0:03:11.820,0:03:14.040 is an object 0:03:14.040,0:03:15.872 with a constructor function. 0:03:15.872,0:03:17.642 And actually,[br]this constructor function, 0:03:17.642,0:03:19.696 if I open this up, 0:03:19.696,0:03:21.468 the function is Dalmation. 0:03:21.468,0:03:25.067 So an object whose function,[br]constructor function, 0:03:25.067,0:03:27.123 is a Dalmation, 0:03:27.123,0:03:28.530 is the prototype 0:03:28.530,0:03:31.350 for all Dalmation objects. 0:03:31.350,0:03:33.450 And there aren't[br]any other properties 0:03:33.450,0:03:36.180 that are really interesting[br]or useful on this object, 0:03:36.180,0:03:38.016 because I haven't set any. 0:03:38.016,0:03:39.360 But for example, 0:03:39.360,0:03:40.470 if I set 0:03:40.470,0:03:42.263 [speaking while typing][br]"Dalmation 0:03:43.917,0:03:45.763 ".prototype 0:03:46.913,0:03:49.551 ".legs 0:03:49.551,0:03:52.148 "= 4...." 0:03:52.148,0:03:53.056 And then I said, 0:03:53.056,0:03:56.010 [speaking while typing][br]"spot.legs," 0:03:56.010,0:03:58.800 all of a sudden, Spot,[br]an existing Dalmation, 0:03:58.800,0:04:01.650 inherits the number of legs 0:04:01.650,0:04:05.336 from the prototype[br]of the Dalmatian class. 0:04:06.370,0:04:11.490 So I can do this[br]for other properties as well. 0:04:11.490,0:04:15.518 So dog.sound is "bark," 0:04:16.990,0:04:21.810 "spot.sound"[br]is now "bark" as well. 0:04:21.810,0:04:24.540 And this will be true[br]for any other dog that I create. 0:04:24.540,0:04:26.550 So for example, 0:04:26.550,0:04:30.641 [speaking while typing][br]Rover is "new Dalmation" as well. 0:04:32.437,0:04:35.942 "rover.legs" is "4", 0:04:36.840,0:04:39.900 "rover.sound" is "bark," 0:04:39.900,0:04:44.167 but I still haven't set a property... 0:04:44.167,0:04:47.563 for speak,[br]so that's still going to be an error. 0:04:48.780,0:04:52.170 So this suggests a way[br]of doing inheritance, 0:04:52.170,0:04:55.260 which is to make sure[br]that all of the properties 0:04:55.260,0:04:58.153 defined for dog are also set 0:04:58.153,0:05:01.969 for the prototype object[br]for the Dalmatian class. 0:05:03.060,0:05:05.495 And there's a really easy way[br]of doing that, 0:05:05.495,0:05:07.710 which is... 0:05:07.710,0:05:11.238 [speaking while typing][br]"Dalmation.prototype 0:05:12.556,0:05:16.000 "= new Dog." 0:05:18.000,0:05:20.760 So Dalmation[br]is going to have a prototype object 0:05:20.760,0:05:23.130 because it's a function, 0:05:23.130,0:05:27.245 but I'm overriding[br]that default generic object 0:05:27.245,0:05:30.040 with a new dog. 0:05:30.815,0:05:33.000 So any new dog that I create 0:05:33.000,0:05:35.032 is going to have[br]all the properties 0:05:35.032,0:05:37.080 that are defined for dog. 0:05:37.080,0:05:40.445 So let's go ahead and--[br]actually let me save this... 0:05:40.445,0:05:42.120 Save... 0:05:42.120,0:05:44.676 and then reload. 0:05:44.676,0:05:46.480 And now, I'm going to create 0:05:46.480,0:05:50.258 [speaking while typing][br]Spot is new Dalmation.