FANDOM


Modifying objects

Modifying Objects is a talk-through in the objects lesson. Pamela demonstrates how to modify the existing properties of an object or add additional properties while a program is running.

Once again using Winston as an example, Pamela begins with a defined Object called "winston" , with one of its properties being "age: 19".  She then demonstrates how this can be modified to increase his age, using the various codes below:

winston.age = 20; //sets age = 20

winston.age = winston.age + 1; //sets age = its prior value +1

winston.age++; //also sets age = its prior value +1

She then goes on to demonstrate how to create a loop that gradually increases Winston's age until it reaches 40, and prints his current age as the loop goes along:

while(winston.age <= 40) {
    text("Winston is " + winston.age + " years old", 10, winston.age*20 - 350);
    winston.age++;
}

Finally, she demonstrates that it's also possible to add new object properties, in this case modifying the above loop to have Winston get married to Winnefer at age 30 (by defining the property "winston.wife"), and have 2 kids, Winston, Jr. and Winstonia, at age 32 (by definining "winston.twins"):

while(winston.age <= 40) {
    text("Winston is " + winston.age + " years old", 10, winston.age*20 - 350);
    winston.age++;
    if (winston.age === 30) {
        winston.wife = "Winnefer";
    }
    if (winston.age === 32) {
        winston.twins = ["Winston, JR", "Winstonia"];
    }
}
☀ All Khan Academy content is available for free at: www.khanacademy.org

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.