Return to Video

Intro to Objects

  • 0:01 - 0:04
    I've written a program to tell you juicy details about
  • 0:04 - 0:07
    Winston, but not too much because Winston likes to keep some
  • 0:07 - 0:12
    mystery. That's just the Winston way. So, let's
  • 0:12 - 0:15
    see how I did this program. I created some variables at the top to store bits of
  • 0:15 - 0:20
    information about him. The first variable holds a number - his age - the second variable holds
  • 0:20 - 0:24
    a string - his eyes - the third variable holds an array of strings,
  • 0:24 - 0:28
    which are things he likes doing, and the last two variables hold
  • 0:28 - 0:32
    strings which describe where he was born. Then
  • 0:32 - 0:36
    down here, I wrote each of these out using the text command and just using the variable
  • 0:36 - 0:40
    name. And of course for the array, I have to access each element of the array
  • 0:40 - 0:43
    using bracket notation. Cool. Now,
  • 0:43 - 0:49
    all five of these variables are describing information about the same thing: Winston
  • 0:49 - 0:52
    But the variables don't know that they're about the same thing
  • 0:52 - 0:56
    And, you know, in Javascript, when you wanna
  • 0:56 - 0:59
    store multiple pieces of information that are related, we have a better way of
  • 0:59 - 1:03
    storing them that's really cool, and it's called an object.
  • 1:03 - 1:07
    So what it means is that instead of 5 variables, we can have a single
  • 1:07 - 1:12
    variable that stores all this information, which is pretty cool.
  • 1:12 - 1:15
    Let's try it out with Winston's information. First, we'll declare the
  • 1:15 - 1:19
    variable and we'll call it Winston. And then
  • 1:19 - 1:23
    we put an open curly bracket - make sure it's curly, not square -
  • 1:23 - 1:27
    and a semicolon. So we've created an object
  • 1:27 - 1:31
    but it has absolutely no information inside of it.
  • 1:32 - 1:36
    So, to add a bit of information, we need to add properties.
  • 1:36 - 1:41
    And each property is a key and a value. For example, age would be age: 19
  • 1:41 - 1:44
    Okay. And then for eyes, we're gonna add a
  • 1:44 - 1:48
    comma, and then eyes: "black". Okay, cool,
  • 1:48 - 1:53
    so now Winston has two properties inside the object.
  • 1:53 - 1:56
    Uh, for likes, we can just go likes: and then I'll
  • 1:56 - 2:00
    just copy paste this from up here... and
  • 2:00 - 2:04
    very nice. So let's look at this. Winston has three properties
  • 2:04 - 2:08
    Every property has a key, which is what's on the right
  • 2:08 - 2:13
    hand side, and a value, which is what's on the left-hand side.
  • 2:13 - 2:16
    For the key, it should follow the same rules as Javascript
  • 2:16 - 2:20
    variable names. No spaces, start it with a letter, all that
  • 2:21 - 2:23
    For the value, it can be any type of value
  • 2:23 - 2:27
    we've seen so far. It could be number, it could be a string, it could be an array
  • 2:27 - 2:33
    It could even be a boolean, so we could add isCool: true, of course
  • 2:34 - 2:38
    In fact, the value could even be another object. So,
  • 2:38 - 2:43
    BirthCity and BirthState. Those really are bits of information about the same
  • 2:43 - 2:47
    thing, which is a single location. And, so I think it be make
  • 2:47 - 2:51
    more sense if we stored it as an object. I'll add another key, birthplace,
  • 2:51 - 2:55
    and then for the value I'm gonna put my curly brackets again
  • 2:55 - 2:58
    and then inside I'll have key for city,
  • 2:58 - 3:02
    "Mountain View", and then state,
  • 3:02 - 3:07
    "California". Great! So now you can see, you can really
  • 3:07 - 3:10
    store very rich information inside an object.
  • 3:11 - 3:15
    All right. So now that we have this nice object that describes all this information about Winston,
  • 3:15 - 3:19
    let's try deleting those old separate variables
  • 3:19 - 3:23
    that didn't know about each other. Okay
  • 3:23 - 3:27
    Um, uh oh! Uh so now we've got an error. And that's because our
  • 3:27 - 3:31
    text commands are referencing the old variables. We need to update
  • 3:31 - 3:35
    them to use information from the object instead. Let's
  • 3:35 - 3:38
    start with just commenting out the last three so that we can
  • 3:38 - 3:42
    do one at a time. Okay. So it says WinstonAge
  • 3:42 - 3:46
    right now. What we need to do is replace that, so we'll type
  • 3:46 - 3:50
    winston, cause that's the variable name. Notice if we
  • 3:50 - 3:54
    just leave it like that, it says object object. That's really
  • 3:54 - 3:57
    gross. That's Javascript telling us that we're trying to turn an entire
  • 3:57 - 4:02
    object into a string value. But instead, we really just
  • 4:02 - 4:06
    wanna access only the age inside of it. So what we do is we put a
  • 4:06 - 4:11
    dot, and then we write the property key, which is "age".
  • 4:11 - 4:15
    Tada! We have the age. We call this "dot notation,"
  • 4:15 - 4:18
    which is where we write the variable name for the object, and then a dot,
  • 4:18 - 4:22
    and then the property key. Okay, so we can
  • 4:22 - 4:26
    go and do more now. Let's uncomment this, and then instead of Winston
  • 4:26 - 4:30
    eyes, we'll just say winston dot eyes, and then
  • 4:30 - 4:34
    for this one, it'll be winston dot likes,
  • 4:34 - 4:38
    and then winston dot likes one, and then
  • 4:38 - 4:43
    for this last one, it's a little more complicated because
  • 4:43 - 4:45
    it's an object inside an object. So we're gonna say
  • 4:45 - 4:49
    winston, and then dot, birthplace,
  • 4:49 - 4:53
    but if we do that it's still just the whole object so then we have to say dot,
  • 4:53 - 4:57
    city. Okay let's do this here... winston,
  • 4:57 - 5:02
    dot, birthplace, dot, state.
  • 5:02 - 5:06
    Tada! So it's really cool cause you can just reach down inside the objects
  • 5:06 - 5:09
    that are in the objects. All right
  • 5:11 - 5:14
    Awesome. So, as you can see,
  • 5:14 - 5:18
    objects are a great way to store a bunch of related bits
  • 5:18 - 5:21
    of information about an object, and then be able to access it later.
  • 5:21 - 5:25
    And when you keep going, you're gonna find out just how awesome objects are!
Title:
Intro to Objects
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:
05:26
dibe_ste approved English subtitles for Intro to Objects
dibe_ste rejected English subtitles for Intro to Objects
dibe_ste edited English subtitles for Intro to Objects
dibe_ste edited English subtitles for Intro to Objects
dibe_ste rejected English subtitles for Intro to Objects
dibe_ste edited English subtitles for Intro to Objects
kramtark edited English subtitles for Intro to Objects

English subtitles

Revisions