Return to Video

Modifying Objects

  • 0:01 - 0:05
    And we're back! This time, our program has the Winston object,
  • 0:05 - 0:10
    but we're only displaying the age of Winston. That's because I wanna show
  • 0:10 - 0:14
    you how we could change Winston's age. Because you know eventually,
  • 0:14 - 0:18
    Winston has to get older. Let's remember what it would be like if we were
  • 0:18 - 0:23
    just using simple variables. We have var winston age equals 19, and then if we
  • 0:23 - 0:27
    wanted to change it, we would say winston age equals 20.
  • 0:27 - 0:31
    and that would change the value stored in that variable. It's really really similar
  • 0:31 - 0:39
    for object properties. We can just say winston dot age equals 20, and then we've changed
  • 0:39 - 0:44
    the value stored in the age property of the Winston object.
  • 0:44 - 0:45
    Cool.
  • 0:45 - 0:49
    So let's see if that worked. We'll take our text command, put it below,
  • 0:49 - 0:54
    change the y, tada! Winston got older. So easy.
  • 0:55 - 0:59
    Um, okay, so now let's try, we were just adding one here
  • 0:59 - 1:07
    let's do that more programatically. Let's say winston dot age equals winston dot age plus one.
  • 1:07 - 1:11
    So what we're saying here is take the previous value of his age
  • 1:11 - 1:16
    add one to it, and then store it in the age property.
  • 1:17 - 1:22
    And that should just end up adding one to whatever the previous value was. Let's see.
  • 1:23 - 1:26
    Display it, tada! He's 21.
  • 1:26 - 1:30
    Getting getting so old. All right. Now, remember we have a shortcut for adding one
  • 1:30 - 1:37
    to variables, and the same shortcut works with object properties so we can say winston dot age plus plus
  • 1:38 - 1:45
    And let's see if that worked. Yup! And that really just did exactly the same thing as this line,
  • 1:45 - 1:49
    it's just a shortcut, so that we don't have to type as much.
  • 1:49 - 1:53
    Now if we look at all this, this really looks like the situation for a loop.
  • 1:53 - 1:58
    We keep on using the same code over and over, and the only thing we're changing is we're adding one
  • 1:58 - 2:03
    to his age, and we're changing the y position so it's on a different line.
  • 2:03 - 2:07
    So, if we used a loop, it could be really easy to show him getting older and older and older
  • 2:07 - 2:13
    and not have to use that much code. Let's say, we'll make a loop to show Winston getting to 40 years old.
  • 2:13 - 2:16
    He doesn't wanna get any older than that because then he'd be wrinkly
  • 2:16 - 2:19
    and we'd have to call him "Wrinkleston." [laugh]
  • 2:19 - 2:20
    All right.
  • 2:20 - 2:27
    So we'll do a while loop and we'll say while winston dot age is less than or equal to 40
  • 2:27 - 2:32
    and then inside here, we're gonna display his age
  • 2:32 - 2:38
    and we're going to add one to his age each time.
  • 2:38 - 2:42
    Okay, that worked, but everything is piled on top of each other, so we need to change the y position
  • 2:42 - 2:53
    each time. And we'll just do winston dot age times 20, um, minus 200, minus 300
  • 2:53 - 3:03
    Okay! and we'll just minus 350. All right. That looks good. Let's delete the old stuff here.
  • 3:03 - 3:10
    Tada! Now we can see Winston getting older and older, but not so old that he'll be a Wrinkleston.
  • 3:10 - 3:16
    Okay. So, now we can see how to change age, we can also add additional information in the
  • 3:16 - 3:22
    Winston object as he gets older. Like, maybe when he turns 30, he'll meet another programmer
  • 3:22 - 3:25
    named Winnefer, and he'll marry her.
  • 3:25 - 3:29
    And no, he's not just gonna marry her because she has such a great name.
  • 3:29 - 3:34
    So what we can do is we can add a property by just saying winston dot, and then the new
  • 3:34 - 3:39
    property key which will be wife, equals Winnefer.
  • 3:39 - 3:44
    Great, but, we only want him to have this wife when he's at, you know, at a good marrying age
  • 3:44 - 3:53
    so we'll say equals 30 and we'll go and move, move that inside here.
  • 3:53 - 3:58
    Great. So now he has a wife, so, you know, they're happy, and then they have some kids
  • 3:58 - 4:04
    a couple years later, so maybe when they're, uh, 32, we'll add some kids to the object
  • 4:04 - 4:08
    to keep track of that. And they have twins, of course, cause they're very productive
  • 4:08 - 4:17
    And Winston's twins will be named, uh, of course Winston Junior and Winstonia.
  • 4:17 - 4:22
    Beautiful. That's a, that's a great family. Great set of names.
  • 4:22 - 4:25
    And so you can see we can add new properties that are strings and arrays
  • 4:25 - 4:29
    and anything we could've had in the original object.
  • 4:29 - 4:34
    And so once this loop finishes, winston dot age will be 40, and winston will
  • 4:34 - 4:41
    have a wife, who's Winnefer, and a twins, which is a set of er, which an array of two names.
  • 4:41 - 4:46
    Huh. That's an awesome loop. But, hey, if you don't like how this story ends for Winston,
  • 4:46 - 4:50
    you could always spin off this program and tell your own story of his future
  • 4:50 - 4:54
    now that you know all about how to change object properties.
Title:
Modifying 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:
04:54

English subtitles

Revisions