[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.00,0:00:02.67,Default,,0000,0000,0000,,Now let's go ahead and make another constructor, Dialogue: 0,0:00:03.17,0:00:06.47,Default,,0000,0000,0000,,so this time, I'm going to create a Dalmatian class... Dialogue: 0,0:00:09.18,0:00:10.89,Default,,0000,0000,0000,,And it's going to be a dog Dialogue: 0,0:00:10.89,0:00:12.06,Default,,0000,0000,0000,,with the color... Dialogue: 0,0:00:15.72,0:00:19.41,Default,,0000,0000,0000,,white with black spots. Dialogue: 0,0:00:22.98,0:00:24.39,Default,,0000,0000,0000,,But obviously, I haven't... Dialogue: 0,0:00:25.05,0:00:27.69,Default,,0000,0000,0000,,created any kind of relationship between "Dalmation" Dialogue: 0,0:00:27.69,0:00:30.54,Default,,0000,0000,0000,,and "dog" just yet, so if I reload, Dialogue: 0,0:00:32.01,0:00:33.00,Default,,0000,0000,0000,,syntax error. Dialogue: 0,0:00:36.18,0:00:38.52,Default,,0000,0000,0000,,Let's see where that is... Dialogue: 0,0:00:39.18,0:00:41.55,Default,,0000,0000,0000,,Oh, I forgot my parentheses. Dialogue: 0,0:00:42.06,0:00:42.99,Default,,0000,0000,0000,,This is a function... Dialogue: 0,0:00:48.75,0:00:52.20,Default,,0000,0000,0000,,So obviously, if I set "spot" Dialogue: 0,0:00:53.94,0:00:54.90,Default,,0000,0000,0000,,to new... Dialogue: 0,0:00:57.51,0:01:00.81,Default,,0000,0000,0000,,Dalmation, Dal-ma-tion... Dialogue: 0,0:01:04.32,0:01:05.10,Default,,0000,0000,0000,,Let's try that again. Dialogue: 0,0:01:06.66,0:01:09.00,Default,,0000,0000,0000,,So now "Spot" is a dog, Dialogue: 0,0:01:09.00,0:01:11.19,Default,,0000,0000,0000,,but only the color property is set. Dialogue: 0,0:01:11.19,0:01:14.85,Default,,0000,0000,0000,,So I can say spot.color, and that works fine, Dialogue: 0,0:01:14.85,0:01:17.10,Default,,0000,0000,0000,,but if I say spot.speak, Dialogue: 0,0:01:18.69,0:01:19.83,Default,,0000,0000,0000,,that doesn't work at all. Dialogue: 0,0:01:20.13,0:01:23.28,Default,,0000,0000,0000,,So, in order to define the inheritance relationship, Dialogue: 0,0:01:24.03,0:01:25.98,Default,,0000,0000,0000,, I need to do something a little odd. Dialogue: 0,0:01:26.04,0:01:28.26,Default,,0000,0000,0000,,So let me give you a little background first. Dialogue: 0,0:01:28.26,0:01:30.66,Default,,0000,0000,0000,,So, if you're used to a language Dialogue: 0,0:01:30.66,0:01:33.99,Default,,0000,0000,0000,,like C ++ or Java or C#, Dialogue: 0,0:01:35.19,0:01:38.19,Default,,0000,0000,0000,,then the way that you define an inheritance relationship Dialogue: 0,0:01:38.22,0:01:41.19,Default,,0000,0000,0000,,is you have a base class, like dog, Dialogue: 0,0:01:41.73,0:01:44.04,Default,,0000,0000,0000,,and then you extend that base class Dialogue: 0,0:01:44.04,0:01:46.68,Default,,0000,0000,0000,,by creating a subclass, like Dalmation. Dialogue: 0,0:01:47.34,0:01:47.91,Default,,0000,0000,0000,,So... Dialogue: 0,0:01:49.47,0:01:51.66,Default,,0000,0000,0000,,The way you do it in JavaScript Dialogue: 0,0:01:51.75,0:01:52.95,Default,,0000,0000,0000,,is a little different. Dialogue: 0,0:01:53.01,0:01:56.22,Default,,0000,0000,0000,,So JavaScript is known as a prototypal Dialogue: 0,0:01:56.22,0:01:57.93,Default,,0000,0000,0000,,or "proto-tip-pal" language, Dialogue: 0,0:01:58.53,0:02:01.32,Default,,0000,0000,0000,,where inheritance is based on prototypes. Dialogue: 0,0:02:01.50,0:02:04.71,Default,,0000,0000,0000,,So what is a prototype? A prototype is an object. Dialogue: 0,0:02:05.34,0:02:08.16,Default,,0000,0000,0000,,So the way you define an inheritance relationship Dialogue: 0,0:02:08.22,0:02:11.79,Default,,0000,0000,0000,,is you set a base prototype, a base object, Dialogue: 0,0:02:12.33,0:02:15.69,Default,,0000,0000,0000,,that the sub-object that you're creating Dialogue: 0,0:02:15.78,0:02:17.25,Default,,0000,0000,0000,,inherits all the properties from. Dialogue: 0,0:02:18.21,0:02:20.64,Default,,0000,0000,0000,,So instead of extending a class, Dialogue: 0,0:02:20.67,0:02:22.89,Default,,0000,0000,0000,,which is a blueprint for a set of objects, Dialogue: 0,0:02:23.37,0:02:26.37,Default,,0000,0000,0000,,instead, you're extending an object Dialogue: 0,0:02:26.64,0:02:29.73,Default,,0000,0000,0000,,by adding additional methods and additional properties Dialogue: 0,0:02:29.73,0:02:32.40,Default,,0000,0000,0000,,to the base set of objects and properties Dialogue: 0,0:02:32.40,0:02:33.60,Default,,0000,0000,0000,,that that object has. Dialogue: 0,0:02:34.53,0:02:36.21,Default,,0000,0000,0000,,So what I want to do, basically, Dialogue: 0,0:02:36.21,0:02:38.19,Default,,0000,0000,0000,,is I want to create a new dog, Dialogue: 0,0:02:39.00,0:02:41.55,Default,,0000,0000,0000,,which is the prototype for all Dalmations, Dialogue: 0,0:02:42.33,0:02:43.98,Default,,0000,0000,0000,,and then I want to inherit Dialogue: 0,0:02:44.61,0:02:46.29,Default,,0000,0000,0000,,that prototype Dialogue: 0,0:02:47.01,0:02:48.84,Default,,0000,0000,0000,,for all Dalmations that I create. Dialogue: 0,0:02:49.95,0:02:52.17,Default,,0000,0000,0000,,The way that works is the constructor, Dialogue: 0,0:02:52.17,0:02:53.85,Default,,0000,0000,0000,,the Dalmation function, Dialogue: 0,0:02:54.39,0:02:57.54,Default,,0000,0000,0000,,has a prototype property, which is an object. Dialogue: 0,0:02:57.99,0:02:59.70,Default,,0000,0000,0000,,So whenever you create a function, Dialogue: 0,0:02:59.70,0:03:01.47,Default,,0000,0000,0000,,there's going to be a prototype property Dialogue: 0,0:03:01.47,0:03:03.57,Default,,0000,0000,0000,,for that function, let's take a look at it. Dialogue: 0,0:03:03.96,0:03:05.19,Default,,0000,0000,0000,,So Dalmation, Dialogue: 0,0:03:06.54,0:03:07.92,Default,,0000,0000,0000,,which is a function.prototype, Dialogue: 0,0:03:11.82,0:03:12.96,Default,,0000,0000,0000,,is an object Dialogue: 0,0:03:14.04,0:03:16.35,Default,,0000,0000,0000,,with a constructor function, and actually, Dialogue: 0,0:03:16.35,0:03:18.78,Default,,0000,0000,0000,,this constructor function, if I open this up, Dialogue: 0,0:03:19.65,0:03:21.42,Default,,0000,0000,0000,,the function is Dalmation. Dialogue: 0,0:03:21.45,0:03:25.11,Default,,0000,0000,0000,,So an object whose function, constructor function, Dialogue: 0,0:03:25.11,0:03:28.44,Default,,0000,0000,0000,,is a Dalmation, is the prototype Dialogue: 0,0:03:28.53,0:03:30.66,Default,,0000,0000,0000,,for all Dalmation objects. Dialogue: 0,0:03:31.35,0:03:33.45,Default,,0000,0000,0000,,And there aren't any other properties Dialogue: 0,0:03:33.45,0:03:36.18,Default,,0000,0000,0000,,that are really interesting or useful on this object, Dialogue: 0,0:03:36.18,0:03:39.09,Default,,0000,0000,0000,,because I haven't set any, but for example, Dialogue: 0,0:03:39.36,0:03:50.61,Default,,0000,0000,0000,,if I set Dalmation.prototype.legs = 4... Dialogue: 0,0:03:52.14,0:03:54.81,Default,,0000,0000,0000,,And then I said, spot.legs, Dialogue: 0,0:03:56.01,0:03:58.80,Default,,0000,0000,0000,,all of a sudden, Spot, an existing Dalmation, Dialogue: 0,0:03:58.80,0:04:01.35,Default,,0000,0000,0000,,inherits the number of legs Dialogue: 0,0:04:01.65,0:04:05.13,Default,,0000,0000,0000,,from the prototype of the Dalmatian class. Dialogue: 0,0:04:06.33,0:04:11.46,Default,,0000,0000,0000,,So I can do this for other properties as well. Dialogue: 0,0:04:11.49,0:04:14.94,Default,,0000,0000,0000,,So dog.sound is bark, Dialogue: 0,0:04:17.55,0:04:20.73,Default,,0000,0000,0000,,spot.sound is now bark as well. Dialogue: 0,0:04:21.81,0:04:24.51,Default,,0000,0000,0000,,And this will be true for any other dog that I create. Dialogue: 0,0:04:24.54,0:04:33.87,Default,,0000,0000,0000,,So for example, Rover is new Dalmation as well. Dialogue: 0,0:04:33.87,0:04:35.76,Default,,0000,0000,0000,,rover.legs is four, Dialogue: 0,0:04:36.84,0:04:39.27,Default,,0000,0000,0000,,rover.sound is bark, Dialogue: 0,0:04:39.90,0:04:42.96,Default,,0000,0000,0000,,but I still haven't set a property Dialogue: 0,0:04:44.10,0:04:47.13,Default,,0000,0000,0000,,for speak, so that's still going to be an error. Dialogue: 0,0:04:48.78,0:04:52.17,Default,,0000,0000,0000,,So this suggests a way of doing inheritance, Dialogue: 0,0:04:52.17,0:04:55.26,Default,,0000,0000,0000,,which is to make sure that all of the properties Dialogue: 0,0:04:55.26,0:04:57.69,Default,,0000,0000,0000,,defined for dog are also set Dialogue: 0,0:04:58.20,0:05:01.65,Default,,0000,0000,0000,,for the prototype object for the Dalmatian class, Dialogue: 0,0:05:03.06,0:05:05.22,Default,,0000,0000,0000,,and there's a really easy way of doing that, Dialogue: 0,0:05:05.46,0:05:06.51,Default,,0000,0000,0000,,which is... Dialogue: 0,0:05:07.71,0:05:15.48,Default,,0000,0000,0000,,Dalmation.prototype = new Dog. Dialogue: 0,0:05:18.00,0:05:20.76,Default,,0000,0000,0000,,So Dalmation is going to have a prototype object Dialogue: 0,0:05:20.76,0:05:22.02,Default,,0000,0000,0000,,because it's a function, Dialogue: 0,0:05:23.13,0:05:27.12,Default,,0000,0000,0000,,but I'm overriding that default generic object Dialogue: 0,0:05:27.18,0:05:29.85,Default,,0000,0000,0000,,with a new dog. Dialogue: 0,0:05:30.81,0:05:32.94,Default,,0000,0000,0000,,So any new dog that I create Dialogue: 0,0:05:33.00,0:05:35.07,Default,,0000,0000,0000,,is going to have all of the properties Dialogue: 0,0:05:35.07,0:05:36.39,Default,,0000,0000,0000,,that are defined for dog. Dialogue: 0,0:05:37.08,0:05:39.66,Default,,0000,0000,0000,,So let's go ahead and-- actually let me save this... Dialogue: 0,0:05:40.32,0:05:41.22,Default,,0000,0000,0000,,Save... Dialogue: 0,0:05:42.12,0:05:43.47,Default,,0000,0000,0000,,and then reload, Dialogue: 0,0:05:44.64,0:05:49.71,Default,,0000,0000,0000,,and now, I'm going to create Spot is new Dalmation.