< 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:07
    So this time,
    I'm going to create a Dalmatian class....
  • 0:09 - 0:13
    And it's going to be a dog
    with the color...
  • 0:16 - 0:20
    [speaking while typing]
    "white with black spots."
  • 0:23 - 0:25
    But obviously, I haven't...
  • 0:25 - 0:28
    created any kind of relationship
    between "Dalmation"
  • 0:28 - 0:32
    and "dog" just yet,
    so if I reload--
  • 0:32 - 0:34
    oh! Syntax error.
  • 0:36 - 0:39
    Let's see where that is....
  • 0:39 - 0:42
    Oh, I forgot my parentheses.
  • 0:42 - 0:44
    This is a function.
  • 0:49 - 0:51
    So obviously, if I set...
  • 0:51 - 0:54
    [speaking while typing]
    "spot"
  • 0:54 - 0:56
    to "new...
  • 0:58 - 1:01
    "Dalmation," Dal-ma-tion....
  • 1:04 - 1:07
    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:18
    but if I say "spot.speak,"
  • 1:19 - 1:20
    that doesn't work at all.
  • 1:20 - 1:24
    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:35
    like C++ or Java or C#,
  • 1:35 - 1:38
    then the way that you define
    an inheritance relationship
  • 1:38 - 1:42
    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:49
    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:59
    or "proto-tip-pal" language,
  • 1:59 - 2:02
    where inheritance is based
    on prototypes.
  • 2:02 - 2:03
    So what is a prototype?
  • 2:03 - 2:05
    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:18
    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:27
    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:35
    that that object has.
  • 2:35 - 2:36
    So what I want to do, basically,
  • 2:36 - 2:39
    is I want to create a new dog,
  • 2:39 - 2:42
    which is the prototype
    for all Dalmations,
  • 2:42 - 2:45
    and then I want to inherit
  • 2:45 - 2:47
    that prototype
  • 2:47 - 2:50
    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:07
    So Dalmation,
  • 3:07 - 3:11
    which is a function.prototype,
  • 3:12 - 3:14
    is an object
  • 3:14 - 3:16
    with a constructor function.
  • 3:16 - 3:18
    And actually,
    this constructor function,
  • 3:18 - 3:20
    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:27
    is a Dalmation,
  • 3:27 - 3:29
    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:38
    because I haven't set any.
  • 3:38 - 3:39
    But for example,
  • 3:39 - 3:40
    if I set
  • 3:40 - 3:42
    [speaking while typing]
    "Dalmation
  • 3:44 - 3:46
    ".prototype
  • 3:47 - 3:50
    ".legs
  • 3:50 - 3:52
    "= 4...."
  • 3:52 - 3:53
    And then I said,
  • 3:53 - 3:56
    [speaking while typing]
    "spot.legs,"
  • 3:56 - 3:59
    all of a sudden, Spot,
    an existing Dalmation,
  • 3:59 - 4:02
    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:16
    So dog.sound is "bark,"
  • 4:17 - 4:22
    "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:27
    So for example,
  • 4:27 - 4:31
    [speaking while typing]
    Rover is "new Dalmation" as well.
  • 4:32 - 4:36
    "rover.legs" is "4",
  • 4:37 - 4:40
    "rover.sound" is "bark,"
  • 4:40 - 4:44
    but I still haven't set a property...
  • 4:44 - 4:48
    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:08
    which is...
  • 5:08 - 5:11
    [speaking while typing]
    "Dalmation.prototype
  • 5:13 - 5:16
    "= new Dog."
  • 5:18 - 5:21
    So Dalmation
    is going to have a prototype object
  • 5:21 - 5:23
    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 the properties
  • 5:35 - 5:37
    that are defined for dog.
  • 5:37 - 5:40
    So let's go ahead and--
    actually let me save this...
  • 5:40 - 5:42
    Save...
  • 5:42 - 5:45
    and then reload.
  • 5:45 - 5:46
    And now, I'm going to create
  • 5:46 - 5:50
    [speaking while typing]
    Spot is new Dalmation.
Title:
233W PrototypeLecture 1 start 5m 52s
Video Language:
English
Duration:
05:53

English subtitles

Revisions Compare revisions