< Return to Video

Object Methods (Video Version)

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

more » « less
Video Language:
English
Duration:
04:54

English subtitles

Revisions