0:00:00.270,0:00:03.334 In the last talk-through, we learned how [br]to make an object type 0:00:03.334,0:00:06.184 to represent our two Winston-like objects 0:00:06.194,0:00:09.234 and then initialize them [br]with the constructors. 0:00:09.234,0:00:13.472 Now, an object type doesn't just [br]have to be associated with properties. 0:00:13.472,0:00:15.866 It can also be associated [br]with functionality. 0:00:15.866,0:00:20.286 Think about the world and all [br]the object types in it, like us humans. 0:00:20.287,0:00:22.307 We all have height and age, 0:00:22.307,0:00:26.287 but we also have things we can do like [br]sleep, and eat, and program. 0:00:26.287,0:00:30.679 And we wanna be able to associate [br]those functions with those object types. 0:00:30.679,0:00:34.279 In this program, which is just where [br]we left off from last time, 0:00:34.289,0:00:39.439 we've a funciton here, drawWinston,[br]that we call on both Winston objects. 0:00:39.439,0:00:44.916 Wouldn't it be neat if we could just [br]attach that to the Winston object type? 0:00:45.046,0:00:48.309 Well we can, and it's easy to do. 0:00:48.569,0:00:52.475 So underneath our constructor, [br]we're gonna write Winston -- 0:00:52.475,0:00:55.575 capital W -- dot prototype. 0:00:55.575,0:00:59.232 And the "prototype", that's a new word [br]that you probably haven't seen before. 0:00:59.232,0:01:04.531 And the prototype is this property [br]of an object that we can attach 0:01:04.531,0:01:09.711 functions to and it will mean that every[br]object that's an instance of that 0:01:09.711,0:01:12.531 will then have those functions on them. 0:01:12.989,0:01:17.632 So we can say dot prototype and then dot, [br]and then the function name. 0:01:17.632,0:01:24.306 So we say draw, equals, and then we can[br]go and take our drawWinston code 0:01:24.306,0:01:28.026 and we can just put it, [br]move it, inside here. 0:01:28.096,0:01:32.811 All right, so now what we've done here [br]is we've attached a draw function 0:01:32.811,0:01:34.938 to our Winston prototype. 0:01:34.938,0:01:40.008 And that means that we should be able[br]to call draw() on any Winston-type object. 0:01:40.008,0:01:45.031 All right, so we should be able to call [br]draw() on winstonTeen or winstonAdult. 0:01:45.331,0:01:49.549 And when we have a function like this, [br]that we can call on an object, 0:01:49.549,0:01:54.653 we actually call that a "method." [br]So you might hear me say "method" now. 0:01:54.653,0:01:58.092 So let's say this is "the draw method." Okay. 0:01:58.092,0:02:03.863 So now we'll delete this, and we'll delete this,[br]and now we're gonna see, can we call draw()? 0:02:03.863,0:02:06.769 winstonTeen.draw() 0:02:07.009,0:02:08.287 Okay. We have an error. 0:02:08.287,0:02:13.577 We've had this error sticking out here, [br]so it says "winstObject is not defined" 0:02:13.577,0:02:18.370 Okay. So, before, we were passing [br]this argument into drawWinston, 0:02:18.370,0:02:22.710 which was the Winston object, [br]but now we're not passing it any more. 0:02:22.710,0:02:27.402 So, we could change this to pass it,[br]and then, let's see, 0:02:27.402,0:02:30.339 what would we pass here?[br]We'd have to pass winstonTeen. 0:02:30.339,0:02:33.901 Okay, that worked, but that seems [br]also really silly. 0:02:33.901,0:02:38.081 I'm already calling draw [br]on the object itself. 0:02:38.081,0:02:41.775 I shouldn't have to pass in[br]the object as well. 0:02:41.775,0:02:43.945 That seems redundant. 0:02:43.945,0:02:45.665 And that's true, [br]we shouldn't have to do that, 0:02:45.665,0:02:48.245 So let's delete this here, [br]and now let's think. 0:02:48.245,0:02:51.544 If we're inside the object, [br]what could we use 0:02:51.544,0:02:54.274 to access the properties of the object? 0:02:54.274,0:02:57.934 Well, you might look up at our constructor[br]and remember that special keyword 0:02:57.934,0:03:04.124 "this" and think "Ahh, what if we just[br]change this, to this!" (laughter) 0:03:04.124,0:03:07.923 So we change winstObject to "this". 0:03:08.383,0:03:12.951 Because we're inside the object right now.[br]This function is being evaluated 0:03:12.951,0:03:16.781 on the object, so the "this" [br]will refer to that current object. 0:03:16.781,0:03:20.361 And so that way we can just say "this" [br]and we'll get access to all 0:03:20.361,0:03:23.041 the properties of this current object. 0:03:23.041,0:03:26.520 And that totally works, see? [br]Isn't that cool? 0:03:26.520,0:03:31.547 So now we can then say winstonAdult.draw() 0:03:31.907,0:03:35.291 Tada! And it will access [br]the properties of winstonAdult 0:03:35.291,0:03:38.021 because that's the object [br]it's being called on. 0:03:38.021,0:03:41.022 So that's what's really cool about [br]this "this" keyword, 0:03:41.022,0:03:44.292 even though it can be kinda [br]confusing to say sometimes. 0:03:45.532,0:03:49.258 All right, so, that was a lot of fun. [br]So let's add another method. 0:03:49.258,0:03:51.908 Okay, so, what else might Winston do? 0:03:51.908,0:03:56.238 Maybe he'll talk. So we'll make a [br]Winston.prototype.talk 0:03:56.238,0:04:00.858 so we can attach as many methods [br]as we want to the prototype. 0:04:00.858,0:04:05.241 So we'll say, "I'm Winston!" 0:04:05.241,0:04:12.751 And then we'll just say this.x+20, [br]and this.y+150. 0:04:12.751,0:04:15.954 And then, y'know, nothing happened, 0:04:15.954,0:04:19.534 but of course that's because I didn't [br]actually call that function yet. 0:04:19.534,0:04:24.048 So, let's make the teen talk, [br]winstonTeen.talk() [inaudible] talk all the time 0:04:24.048,0:04:30.299 Okay. I'm Winston, tada! [br]And then winstonAdult.talk() 0:04:30.299,0:04:31.928 Tada! 0:04:31.928,0:04:36.438 All right, so now we have this Winston [br]object type that has properties: 0:04:36.438,0:04:41.496 nickname, age, x, y; and it has[br]functionality: behaviors, methods; 0:04:41.496,0:04:44.246 that act differently depending on[br]the properties, 0:04:44.246,0:04:48.146 and we can create as many instances [br]of Winstons as we want 0:04:48.146,0:04:50.649 and call any of these methods on it. 0:04:50.649,0:04:52.873 It's pretty cool, huh?