[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.27,0:00:03.33,Default,,0000,0000,0000,,In the last talk-through, we learned how \Nto make an object type Dialogue: 0,0:00:03.33,0:00:06.18,Default,,0000,0000,0000,,to represent our two Winston-like objects Dialogue: 0,0:00:06.19,0:00:09.23,Default,,0000,0000,0000,,and then initialize them \Nwith the constructors. Dialogue: 0,0:00:09.23,0:00:13.47,Default,,0000,0000,0000,,Now, an object type doesn't just \Nhave to be associated with properties. Dialogue: 0,0:00:13.47,0:00:15.87,Default,,0000,0000,0000,,It can also be associated \Nwith functionality. Dialogue: 0,0:00:15.87,0:00:20.29,Default,,0000,0000,0000,,Think about the world and all \Nthe object types in it, like us humans. Dialogue: 0,0:00:20.29,0:00:22.31,Default,,0000,0000,0000,,We all have height and age, Dialogue: 0,0:00:22.31,0:00:26.29,Default,,0000,0000,0000,,but we also have things we can do like \Nsleep, and eat, and program. Dialogue: 0,0:00:26.29,0:00:30.68,Default,,0000,0000,0000,,And we wanna be able to associate \Nthose functions with those object types. Dialogue: 0,0:00:30.68,0:00:34.28,Default,,0000,0000,0000,,In this program, which is just where \Nwe left off from last time, Dialogue: 0,0:00:34.29,0:00:39.44,Default,,0000,0000,0000,,we've a funciton here, drawWinston,\Nthat we call on both Winston objects. Dialogue: 0,0:00:39.44,0:00:44.92,Default,,0000,0000,0000,,Wouldn't it be neat if we could just \Nattach that to the Winston object type? Dialogue: 0,0:00:45.05,0:00:48.31,Default,,0000,0000,0000,,Well we can, and it's easy to do. Dialogue: 0,0:00:48.57,0:00:52.48,Default,,0000,0000,0000,,So underneath our constructor, \Nwe're gonna write Winston -- Dialogue: 0,0:00:52.48,0:00:55.58,Default,,0000,0000,0000,,capital W -- dot prototype. Dialogue: 0,0:00:55.58,0:00:59.23,Default,,0000,0000,0000,,And the "prototype", that's a new word \Nthat you probably haven't seen before. Dialogue: 0,0:00:59.23,0:01:04.53,Default,,0000,0000,0000,,And the prototype is this property \Nof an object that we can attach Dialogue: 0,0:01:04.53,0:01:09.71,Default,,0000,0000,0000,,functions to and it will mean that every\Nobject that's an instance of that Dialogue: 0,0:01:09.71,0:01:12.53,Default,,0000,0000,0000,,will then have those functions on them. Dialogue: 0,0:01:12.99,0:01:17.63,Default,,0000,0000,0000,,So we can say dot prototype and then dot, \Nand then the function name. Dialogue: 0,0:01:17.63,0:01:24.31,Default,,0000,0000,0000,,So we say draw, equals, and then we can\Ngo and take our drawWinston code Dialogue: 0,0:01:24.31,0:01:28.03,Default,,0000,0000,0000,,and we can just put it, \Nmove it, inside here. Dialogue: 0,0:01:28.10,0:01:32.81,Default,,0000,0000,0000,,All right, so now what we've done here \Nis we've attached a draw function Dialogue: 0,0:01:32.81,0:01:34.94,Default,,0000,0000,0000,,to our Winston prototype. Dialogue: 0,0:01:34.94,0:01:40.01,Default,,0000,0000,0000,,And that means that we should be able\Nto call draw() on any Winston-type object. Dialogue: 0,0:01:40.01,0:01:45.03,Default,,0000,0000,0000,,All right, so we should be able to call \Ndraw() on winstonTeen or winstonAdult. Dialogue: 0,0:01:45.33,0:01:49.55,Default,,0000,0000,0000,,And when we have a function like this, \Nthat we can call on an object, Dialogue: 0,0:01:49.55,0:01:54.65,Default,,0000,0000,0000,,we actually call that a "method." \NSo you might hear me say "method" now. Dialogue: 0,0:01:54.65,0:01:58.09,Default,,0000,0000,0000,,So let's say this is "the draw method." Okay. Dialogue: 0,0:01:58.09,0:02:03.86,Default,,0000,0000,0000,,So now we'll delete this, and we'll delete this,\Nand now we're gonna see, can we call draw()? Dialogue: 0,0:02:03.86,0:02:06.77,Default,,0000,0000,0000,,winstonTeen.draw() Dialogue: 0,0:02:07.01,0:02:08.29,Default,,0000,0000,0000,,Okay. We have an error. Dialogue: 0,0:02:08.29,0:02:13.58,Default,,0000,0000,0000,,We've had this error sticking out here, \Nso it says "winstObject is not defined" Dialogue: 0,0:02:13.58,0:02:18.37,Default,,0000,0000,0000,,Okay. So, before, we were passing \Nthis argument into drawWinston, Dialogue: 0,0:02:18.37,0:02:22.71,Default,,0000,0000,0000,,which was the Winston object, \Nbut now we're not passing it any more. Dialogue: 0,0:02:22.71,0:02:27.40,Default,,0000,0000,0000,,So, we could change this to pass it,\Nand then, let's see, Dialogue: 0,0:02:27.40,0:02:30.34,Default,,0000,0000,0000,,what would we pass here?\NWe'd have to pass winstonTeen. Dialogue: 0,0:02:30.34,0:02:33.90,Default,,0000,0000,0000,,Okay, that worked, but that seems \Nalso really silly. Dialogue: 0,0:02:33.90,0:02:38.08,Default,,0000,0000,0000,,I'm already calling draw \Non the object itself. Dialogue: 0,0:02:38.08,0:02:41.78,Default,,0000,0000,0000,,I shouldn't have to pass in\Nthe object as well. Dialogue: 0,0:02:41.78,0:02:43.94,Default,,0000,0000,0000,,That seems redundant. Dialogue: 0,0:02:43.94,0:02:45.66,Default,,0000,0000,0000,,And that's true, \Nwe shouldn't have to do that, Dialogue: 0,0:02:45.66,0:02:48.24,Default,,0000,0000,0000,,So let's delete this here, \Nand now let's think. Dialogue: 0,0:02:48.24,0:02:51.54,Default,,0000,0000,0000,,If we're inside the object, \Nwhat could we use Dialogue: 0,0:02:51.54,0:02:54.27,Default,,0000,0000,0000,,to access the properties of the object? Dialogue: 0,0:02:54.27,0:02:57.93,Default,,0000,0000,0000,,Well, you might look up at our constructor\Nand remember that special keyword Dialogue: 0,0:02:57.93,0:03:04.12,Default,,0000,0000,0000,,"this" and think "Ahh, what if we just\Nchange this, to this!" (laughter) Dialogue: 0,0:03:04.12,0:03:07.92,Default,,0000,0000,0000,,So we change winstObject to "this". Dialogue: 0,0:03:08.38,0:03:12.95,Default,,0000,0000,0000,,Because we're inside the object right now.\NThis function is being evaluated Dialogue: 0,0:03:12.95,0:03:16.78,Default,,0000,0000,0000,,on the object, so the "this" \Nwill refer to that current object. Dialogue: 0,0:03:16.78,0:03:20.36,Default,,0000,0000,0000,,And so that way we can just say "this" \Nand we'll get access to all Dialogue: 0,0:03:20.36,0:03:23.04,Default,,0000,0000,0000,,the properties of this current object. Dialogue: 0,0:03:23.04,0:03:26.52,Default,,0000,0000,0000,,And that totally works, see? \NIsn't that cool? Dialogue: 0,0:03:26.52,0:03:31.55,Default,,0000,0000,0000,,So now we can then say winstonAdult.draw() Dialogue: 0,0:03:31.91,0:03:35.29,Default,,0000,0000,0000,,Tada! And it will access \Nthe properties of winstonAdult Dialogue: 0,0:03:35.29,0:03:38.02,Default,,0000,0000,0000,,because that's the object \Nit's being called on. Dialogue: 0,0:03:38.02,0:03:41.02,Default,,0000,0000,0000,,So that's what's really cool about \Nthis "this" keyword, Dialogue: 0,0:03:41.02,0:03:44.29,Default,,0000,0000,0000,,even though it can be kinda \Nconfusing to say sometimes. Dialogue: 0,0:03:45.53,0:03:49.26,Default,,0000,0000,0000,,All right, so, that was a lot of fun. \NSo let's add another method. Dialogue: 0,0:03:49.26,0:03:51.91,Default,,0000,0000,0000,,Okay, so, what else might Winston do? Dialogue: 0,0:03:51.91,0:03:56.24,Default,,0000,0000,0000,,Maybe he'll talk. So we'll make a \NWinston.prototype.talk Dialogue: 0,0:03:56.24,0:04:00.86,Default,,0000,0000,0000,,so we can attach as many methods \Nas we want to the prototype. Dialogue: 0,0:04:00.86,0:04:05.24,Default,,0000,0000,0000,,So we'll say, "I'm Winston!" Dialogue: 0,0:04:05.24,0:04:12.75,Default,,0000,0000,0000,,And then we'll just say this.x+20, \Nand this.y+150. Dialogue: 0,0:04:12.75,0:04:15.95,Default,,0000,0000,0000,,And then, y'know, nothing happened, Dialogue: 0,0:04:15.95,0:04:19.53,Default,,0000,0000,0000,,but of course that's because I didn't \Nactually call that function yet. Dialogue: 0,0:04:19.53,0:04:24.05,Default,,0000,0000,0000,,So, let's make the teen talk, \NwinstonTeen.talk() [inaudible] talk all the time Dialogue: 0,0:04:24.05,0:04:30.30,Default,,0000,0000,0000,,Okay. I'm Winston, tada! \NAnd then winstonAdult.talk() Dialogue: 0,0:04:30.30,0:04:31.93,Default,,0000,0000,0000,,Tada! Dialogue: 0,0:04:31.93,0:04:36.44,Default,,0000,0000,0000,,All right, so now we have this Winston \Nobject type that has properties: Dialogue: 0,0:04:36.44,0:04:41.50,Default,,0000,0000,0000,,nickname, age, x, y; and it has\Nfunctionality: behaviors, methods; Dialogue: 0,0:04:41.50,0:04:44.25,Default,,0000,0000,0000,,that act differently depending on\Nthe properties, Dialogue: 0,0:04:44.25,0:04:48.15,Default,,0000,0000,0000,,and we can create as many instances \Nof Winstons as we want Dialogue: 0,0:04:48.15,0:04:50.65,Default,,0000,0000,0000,,and call any of these methods on it. Dialogue: 0,0:04:50.65,0:04:52.87,Default,,0000,0000,0000,,It's pretty cool, huh?