< Return to Video

233W PrototypeLecture 1 start 5m 52s

  • 0:00 - 0:03
    Now let's go ahead and make another constructor,
  • 0:03 - 0:06
    so this time, I'm going to create a Dalmatian class...
  • 0:09 - 0:11
    And it's going to be a dog
  • 0:11 - 0:12
    with the color...
  • 0:16 - 0:19
    white with black spots.
  • 0:23 - 0:24
    But obviously, I haven't...
  • 0:25 - 0:28
    created any kind of relationship between "Dalmation"
  • 0:28 - 0:31
    and "dog" just yet, so if I reload,
  • 0:32 - 0:33
    syntax error.
  • 0:36 - 0:39
    Let's see where that is...
  • 0:39 - 0:42
    Oh, I forgot my parentheses.
  • 0:42 - 0:43
    This is a function...
  • 0:49 - 0:52
    So obviously, if I set "spot"
  • 0:54 - 0:55
    to new...
  • 0:58 - 1:01
    Dalmation, Dal-ma-tion...
  • 1:04 - 1:05
    Let's try that again.
  • 1:07 - 1:09
    So now "Spot" is a dog,
  • 1:09 - 1:11
    but only the color property is set.
  • 1:11 - 1:15
    So I can say spot.color, and that works fine,
  • 1:15 - 1:17
    but if I say spot.speak,
  • 1:19 - 1:20
    that doesn't work at all.
  • 1:20 - 1:23
    So, in order to define the inheritance relationship,
  • 1:24 - 1:26
    I need to do something a little odd.
  • 1:26 - 1:28
    So let me give you a little background first.
  • 1:28 - 1:31
    So, if you're used to a language
  • 1:31 - 1:34
    like C ++ or Java or C#,
  • 1:35 - 1:38
    then the way that you define an inheritance relationship
  • 1:38 - 1:41
    is you have a base class, like dog,
  • 1:42 - 1:44
    and then you extend that base class
  • 1:44 - 1:47
    by creating a subclass, like Dalmation.
  • 1:47 - 1:48
    So...
  • 1:49 - 1:52
    The way you do it in JavaScript
  • 1:52 - 1:53
    is a little different.
  • 1:53 - 1:56
    So JavaScript is known as a prototypal
  • 1:56 - 1:58
    or "proto-tip-pal" language,
  • 1:59 - 2:01
    where inheritance is based on prototypes.
  • 2:02 - 2:05
    So what is a prototype? A prototype is an object.
  • 2:05 - 2:08
    So the way you define an inheritance relationship
  • 2:08 - 2:12
    is you set a base prototype, a base object,
  • 2:12 - 2:16
    that the sub-object that you're creating
  • 2:16 - 2:17
    inherits all the properties from.
  • 2:18 - 2:21
    So instead of extending a class,
  • 2:21 - 2:23
    which is a blueprint for a set of objects,
  • 2:23 - 2:26
    instead, you're extending an object
  • 2:27 - 2:30
    by adding additional methods and additional properties
  • 2:30 - 2:32
    to the base set of objects and properties
  • 2:32 - 2:34
    that that object has.
  • 2:35 - 2:36
    So what I want to do, basically,
  • 2:36 - 2:38
    is I want to create a new dog,
  • 2:39 - 2:42
    which is the prototype for all Dalmations,
  • 2:42 - 2:44
    and then I want to inherit
  • 2:45 - 2:46
    that prototype
  • 2:47 - 2:49
    for all Dalmations that I create.
  • 2:50 - 2:52
    The way that works is the constructor,
  • 2:52 - 2:54
    the Dalmation function,
  • 2:54 - 2:58
    has a prototype property, which is an object.
  • 2:58 - 3:00
    So whenever you create a function,
  • 3:00 - 3:01
    there's going to be a prototype property
  • 3:01 - 3:04
    for that function, let's take a look at it.
  • 3:04 - 3:05
    So Dalmation,
  • 3:07 - 3:08
    which is a function.prototype,
  • 3:12 - 3:13
    is an object
  • 3:14 - 3:16
    with a constructor function, and actually,
  • 3:16 - 3:19
    this constructor function, if I open this up,
  • 3:20 - 3:21
    the function is Dalmation.
  • 3:21 - 3:25
    So an object whose function, constructor function,
  • 3:25 - 3:28
    is a Dalmation, is the prototype
  • 3:29 - 3:31
    for all Dalmation objects.
  • 3:31 - 3:33
    And there aren't any other properties
  • 3:33 - 3:36
    that are really interesting or useful on this object,
  • 3:36 - 3:39
    because I haven't set any, but for example,
  • 3:39 - 3:51
    if I set Dalmation.prototype.legs = 4...
  • 3:52 - 3:55
    And then I said, spot.legs,
  • 3:56 - 3:59
    all of a sudden, Spot, an existing Dalmation,
  • 3:59 - 4:01
    inherits the number of legs
  • 4:02 - 4:05
    from the prototype of the Dalmatian class.
  • 4:06 - 4:11
    So I can do this for other properties as well.
  • 4:11 - 4:15
    So dog.sound is bark,
  • 4:18 - 4:21
    spot.sound is now bark as well.
  • 4:22 - 4:25
    And this will be true for any other dog that I create.
  • 4:25 - 4:34
    So for example, Rover is new Dalmation as well.
  • 4:34 - 4:36
    rover.legs is four,
  • 4:37 - 4:39
    rover.sound is bark,
  • 4:40 - 4:43
    but I still haven't set a property
  • 4:44 - 4:47
    for speak, so that's still going to be an error.
  • 4:49 - 4:52
    So this suggests a way of doing inheritance,
  • 4:52 - 4:55
    which is to make sure that all of the properties
  • 4:55 - 4:58
    defined for dog are also set
  • 4:58 - 5:02
    for the prototype object for the Dalmatian class,
  • 5:03 - 5:05
    and there's a really easy way of doing that,
  • 5:05 - 5:07
    which is...
  • 5:08 - 5:15
    Dalmation.prototype = new Dog.
  • 5:18 - 5:21
    So Dalmation is going to have a prototype object
  • 5:21 - 5:22
    because it's a function,
  • 5:23 - 5:27
    but I'm overriding that default generic object
  • 5:27 - 5:30
    with a new dog.
  • 5:31 - 5:33
    So any new dog that I create
  • 5:33 - 5:35
    is going to have all of the properties
  • 5:35 - 5:36
    that are defined for dog.
  • 5:37 - 5:40
    So let's go ahead and-- actually let me save this...
  • 5:40 - 5:41
    Save...
  • 5:42 - 5:43
    and then reload,
  • 5:45 - 5:50
    and now, I'm going to create Spot is new Dalmation.
Title:
233W PrototypeLecture 1 start 5m 52s
Video Language:
English
Duration:
05:53

English subtitles

Revisions Compare revisions