Object Methods (Video Version)
-
0:00 - 0:03In the last talk-through, we learned how
to make an object type -
0:03 - 0:06to represent our two Winston-like objects
-
0:06 - 0:09and then initialize them
with the constructors. -
0:09 - 0:13Now, an object type doesn't just
have to be associated with properties. -
0:13 - 0:16It can also be associated
with functionality. -
0:16 - 0:20Think about the world and all
the object types in it, like us humans. -
0:20 - 0:22We all have height and age,
-
0:22 - 0:26but we also have things we can do like
sleep, and eat, and program. -
0:26 - 0:31And we wanna be able to associate
those functions with those object types. -
0:31 - 0:34In this program, which is just where
we left off from last time, -
0:34 - 0:39we've a funciton here, drawWinston,
that we call on both Winston objects. -
0:39 - 0:45Wouldn't it be neat if we could just
attach that to the Winston object type? -
0:45 - 0:48Well we can, and it's easy to do.
-
0:49 - 0:52So underneath our constructor,
we're gonna write Winston -- -
0:52 - 0:56capital W -- dot prototype.
-
0:56 - 0:59And the "prototype", that's a new word
that you probably haven't seen before. -
0:59 - 1:05And the prototype is this property
of an object that we can attach -
1:05 - 1:10functions to and it will mean that every
object that's an instance of that -
1:10 - 1:13will then have those functions on them.
-
1:13 - 1:18So we can say dot prototype and then dot,
and then the function name. -
1:18 - 1:24So we say draw, equals, and then we can
go and take our drawWinston code -
1:24 - 1:28and we can just put it,
move it, inside here. -
1:28 - 1:33All right, so now what we've done here
is we've attached a draw function -
1:33 - 1:35to our Winston prototype.
-
1:35 - 1:40And that means that we should be able
to call draw() on any Winston-type object. -
1:40 - 1:45All right, so we should be able to call
draw() on winstonTeen or winstonAdult. -
1:45 - 1:50And when we have a function like this,
that we can call on an object, -
1:50 - 1:55we actually call that a "method."
So you might hear me say "method" now. -
1:55 - 1:58So let's say this is "the draw method." Okay.
-
1:58 - 2:04So now we'll delete this, and we'll delete this,
and now we're gonna see, can we call draw()? -
2:04 - 2:07winstonTeen.draw()
-
2:07 - 2:08Okay. We have an error.
-
2:08 - 2:14We've had this error sticking out here,
so it says "winstObject is not defined" -
2:14 - 2:18Okay. So, before, we were passing
this argument into drawWinston, -
2:18 - 2:23which was the Winston object,
but now we're not passing it any more. -
2:23 - 2:27So, we could change this to pass it,
and then, let's see, -
2:27 - 2:30what would we pass here?
We'd have to pass winstonTeen. -
2:30 - 2:34Okay, that worked, but that seems
also really silly. -
2:34 - 2:38I'm already calling draw
on the object itself. -
2:38 - 2:42I shouldn't have to pass in
the object as well. -
2:42 - 2:44That seems redundant.
-
2:44 - 2:46And that's true,
we shouldn't have to do that, -
2:46 - 2:48So let's delete this here,
and now let's think. -
2:48 - 2:52If we're inside the object,
what could we use -
2:52 - 2:54to access the properties of the object?
-
2:54 - 2:58Well, you might look up at our constructor
and remember that special keyword -
2:58 - 3:04"this" and think "Ahh, what if we just
change this, to this!" (laughter) -
3:04 - 3:08So we change winstObject to "this".
-
3:08 - 3:13Because we're inside the object right now.
This function is being evaluated -
3:13 - 3:17on the object, so the "this"
will refer to that current object. -
3:17 - 3:20And so that way we can just say "this"
and we'll get access to all -
3:20 - 3:23the properties of this current object.
-
3:23 - 3:27And that totally works, see?
Isn't that cool? -
3:27 - 3:32So now we can then say winstonAdult.draw()
-
3:32 - 3:35Tada! And it will access
the properties of winstonAdult -
3:35 - 3:38because that's the object
it's being called on. -
3:38 - 3:41So that's what's really cool about
this "this" keyword, -
3:41 - 3:44even though it can be kinda
confusing to say sometimes. -
3:46 - 3:49All right, so, that was a lot of fun.
So let's add another method. -
3:49 - 3:52Okay, so, what else might Winston do?
-
3:52 - 3:56Maybe he'll talk. So we'll make a
Winston.prototype.talk -
3:56 - 4:01so we can attach as many methods
as we want to the prototype. -
4:01 - 4:05So we'll say, "I'm Winston!"
-
4:05 - 4:13And then we'll just say this.x+20,
and this.y+150. -
4:13 - 4:16And then, y'know, nothing happened,
-
4:16 - 4:20but of course that's because I didn't
actually call that function yet. -
4:20 - 4:24So, let's make the teen talk,
winstonTeen.talk() [inaudible] talk all the time -
4:24 - 4:30Okay. I'm Winston, tada!
And then winstonAdult.talk() -
4:30 - 4:32Tada!
-
4:32 - 4:36All right, so now we have this Winston
object type that has properties: -
4:36 - 4:41nickname, age, x, y; and it has
functionality: behaviors, methods; -
4:41 - 4:44that act differently depending on
the properties, -
4:44 - 4:48and we can create as many instances
of Winstons as we want -
4:48 - 4:51and call any of these methods on it.
-
4:51 - 4:53It's pretty cool, huh?
- Title:
- Object Methods (Video Version)
- Description:
-
This is just a screen grab of our interactive coding talk-through, prepared to make captioning and translation easier. It is better to watch our talk-throughs here:
https://www.khanacademy.org/cs/programming/ - Video Language:
- English
- Duration:
- 04:54
Tanya Higgins approved English subtitles for Object Methods (Video Version) | ||
Tanya Higgins edited English subtitles for Object Methods (Video Version) | ||
Tanya Higgins edited English subtitles for Object Methods (Video Version) | ||
Tanya Higgins edited English subtitles for Object Methods (Video Version) | ||
Tanya Higgins edited English subtitles for Object Methods (Video Version) | ||
Tanya Higgins edited English subtitles for Object Methods (Video Version) | ||
Tanya Higgins edited English subtitles for Object Methods (Video Version) | ||
Tanya Higgins edited English subtitles for Object Methods (Video Version) |