Object Inheritance (Video Version)
-
0:01 - 0:03We're back with our program
that creates Winstons -
0:03 - 0:06but I've added a new object type, Hopper,
-
0:06 - 0:09because Hopper was feeling
a little left out. -
0:09 - 0:12Now I define Hopper
the same way that I define Winston. -
0:12 - 0:15Starting off with the constructer function
and taking the same properties -
0:15 - 0:21and then draw, and talk, and then
I also added another method called Horray -
0:21 - 0:25because Hoppers really like to celebrate
and Winstons don't really at all. -
0:25 - 0:30Now at the bottom of the function,
I've created two new Hopper objects: -
0:30 - 0:32Little Hopper and Big Hopper
-
0:32 - 0:37and drawn them and called talk on one
and Horray on the other. -
0:37 - 0:38So that's pretty neat
-
0:38 - 0:43Now, if we look at this code up here,
you might notice something interesting. -
0:43 - 0:47The code for hopper is very similar
to the code for winston. -
0:47 - 0:51Particularly look at this constructor.
I don't know if you remember but that is -
0:51 - 0:55exactly the same code
as our Winston constructor function. -
0:55 - 0:58And then this talk function, is also
exactly the same code -
0:58 - 1:04as our Winston talk function,
and they also both have draw functions. -
1:04 - 1:07So there's a lot of things in common
about these two object types -
1:07 - 1:10and that makes sense because
Hopper and Winston -
1:10 - 1:13they're two very similar object types
in our world. -
1:13 - 1:18If you think about the real world,
outside the computer, -
1:18 - 1:21most object types shares similarities
with other object types. -
1:21 - 1:26Like in the animal kingdom.
All animals are similar in some ways. -
1:26 - 1:30And then we have different types
of animals, like humans. -
1:30 - 1:34Humans share those similarities but also
have their own unique similarities. -
1:34 - 1:38So we could say that animal
is an object type -
1:38 - 1:42that human object types
inherit functionality from. -
1:42 - 1:44We don't completely start from scratch.
-
1:44 - 1:47We add on top of the functionality
that we get from being an animal. -
1:47 - 1:52Like all animals make noises,
but humans also make language. -
1:52 - 1:56So this concept of object inheritance
is really useful in programming too. -
1:56 - 2:01And we can create a chain of
object inheritance in our Javascript. -
2:01 - 2:05So to do this when you think about
what's shared about our object types. -
2:05 - 2:07And come up with a name for that
-
2:07 - 2:10'cause we're going to create
a new object type that represents -
2:10 - 2:12the base object.
So let's call them creatures. -
2:12 - 2:14They're both creatures.
-
2:14 - 2:16So we say var creature equals...
-
2:16 - 2:20And now we need our constructer function
So let's go and just steal Hopper's -
2:20 - 2:24since that's the same thing
that Winston has. Alright. -
2:24 - 2:29And then... let's see. Now we want to...
What do we want to do next? -
2:29 - 2:33Maybe we want to add
the "talk" function on it. -
2:33 - 2:36Talk function, we could just
steal Hoppper's. -
2:36 - 2:40But of course we need to have it on
the creature's prototype instead. -
2:40 - 2:46Okay, cool. So now we have
this creature object type. -
2:46 - 2:48But we need to actually tell Hopper
-
2:48 - 2:53that Hopper should actually
base it's functionality off of creature. -
2:53 - 2:56So we can do that
by writing this line here. -
2:56 - 3:05We'll say "Hopper.prototype
equals object.create, creature.prototype" -
3:05 - 3:10So what this line does, tells Javascript
to base Hopper's prototype, -
3:10 - 3:16so to base all of Hopper's functionality,
off of the creature prototype. -
3:16 - 3:20That means that every time it looks
for a function on a Hopper, -
3:20 - 3:24it'll look at Hopper's prototype first,
but then if it doesn't find that -
3:24 - 3:27it'll actually look and see
if it was on the creature prototype. -
3:27 - 3:30And this is what we call
the prototype chain. -
3:30 - 3:32Now, once we've done this
we should actually be able to -
3:32 - 3:38delete the talk function on Hopper
because it already exists on creature. -
3:38 - 3:42Which is up the prototype chain,
so let's try it. Ready? ♪Dun dun dun♪ -
3:42 - 3:48It worked! And it works because
it finds it on creature prototype instead. -
3:49 - 3:52So let's try deleting it on Winston too.
-
3:53 - 3:58Okay. It didn't work it says
"object has no method 'talk'". -
3:58 - 4:01And why is that?
Well we have our Winston constructer -
4:01 - 4:03and draw and we took away the talk.
-
4:03 - 4:07Well, you notice we forgot to actually
tell Winson's prototype -
4:07 - 4:09to be based off the creature prototype.
-
4:09 - 4:14So we need that very important line.
Winston.prototype=object.create -
4:14 - 4:17(Creature.prototype)
-
4:18 - 4:20Ta Da!
And notice something important. -
4:20 - 4:23I have this line after
the constructor function -
4:23 - 4:27but before I add anything else
to the Winston prototype. -
4:27 - 4:30So that's usually what you want to do.
You want to tell it -
4:30 - 4:32just as your starting off immediately:
-
4:32 - 4:34what your initial prototype
will be based on. -
4:34 - 4:37But then we keep adding more stuff
to it's prototype. -
4:37 - 4:39Because there could be some things
-
4:39 - 4:42that are unique to Winstons
or unique to Hoppers -
4:42 - 4:43that aren't on creatures.
-
4:43 - 4:46And that's totally cool
that you can define those. -
4:46 - 4:50Alright. Now, if we look at this
we still have some repeated code. -
4:50 - 4:52The constructor code.
-
4:52 - 4:54Right? We have this all three times.
-
4:54 - 4:59So could we just delete it?
Let's try it. -
5:01 - 5:04Okay. Hmm...
Doesn't seem like that worked -
5:04 - 5:06'Cause our Hopper shows up
in the upper left corner. -
5:06 - 5:08It kind of forgot everything about itself.
-
5:08 - 5:11And this is because Javascript
does not assume you want -
5:11 - 5:15the same constructor even if you want
to base the prototype off of it. -
5:15 - 5:19It lets you define your own
constructor for these objects. -
5:19 - 5:26But it also does give you an easy way
to call a constructor from a sub-object -
5:26 - 5:28The way we can do this is,
we will write -
5:28 - 5:36creature.call(this,nickname,age,x,y)
-
5:36 - 5:41Now what this does--notice it worked. yay.
And what it did actually -
5:41 - 5:44it's calling the creature function,
--the construction function. -
5:44 - 5:47It's calling that function
but it's passing and saying, -
5:47 - 5:50okay you should call this
constructor function as if -
5:50 - 5:54it was being called from
this Hopper object -
5:54 - 5:57and as if it's being called with
these arguments. -
5:57 - 5:59These are arguments that
the Hopper got called with. -
5:59 - 6:04And that will end up just executing
this code as if it was right here. -
6:04 - 6:07And that's exactly what we want.
And it worked. -
6:07 - 6:09And we can go ahead and
-
6:09 - 6:15copy this line into
the Winston constructor too. -
6:15 - 6:17And it works. Yay!
-
6:17 - 6:20Alright. So check this out.
We've encapsulated all our shared -
6:20 - 6:23properties and functionalities
about objects in this single base -
6:23 - 6:25object type: creature
-
6:25 - 6:28And we've made two object types
that extend from that base object. -
6:28 - 6:32They inherit some functionality
but they add their own too. -
6:32 - 6:33And the cool thing about this
-
6:33 - 6:36is that we can change the shared
functionality in a single place. -
6:36 - 6:41Like if we wanted to change age again.
We could say "+ yrs old". -
6:41 - 6:44Cool, now everybody has
"yrs old" at the end of it. -
6:44 - 6:49Or we could change the "talk" functions
and be like "suppp?!". Woo! -
6:49 - 6:53And now both Winstons and Hoppers
are saying "sup". -
6:53 - 6:54So now that you've seen how to
-
6:54 - 6:57create object types
and inherit from object types, -
6:57 - 6:59you can start thinking
about how this might be useful -
6:59 - 7:02in your drawings and
animations and simulations and games. -
7:02 - 7:05For example, maybe you have a game and
you have many types of characters in it -
7:05 - 7:08All of them can run
but only some of them can jump. -
7:08 - 7:12That's a perfect place for some
object types and some inheritance. -
7:12 - 7:16But I bet you can think of
tons of more ways as well.
- Title:
- Object Inheritance (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:
- 07:17
Shaquille Robinson approved English subtitles for Object Inheritance (Video Version) | ||
Shaquille Robinson edited English subtitles for Object Inheritance (Video Version) | ||
Shaquille Robinson edited English subtitles for Object Inheritance (Video Version) | ||
Shaquille Robinson edited English subtitles for Object Inheritance (Video Version) | ||
Shaquille Robinson edited English subtitles for Object Inheritance (Video Version) | ||
Shaquille Robinson edited English subtitles for Object Inheritance (Video Version) | ||
Shaquille Robinson edited English subtitles for Object Inheritance (Video Version) | ||
Shaquille Robinson edited English subtitles for Object Inheritance (Video Version) |