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