Back to Methods Change State
OOP Studio Ages 9-15

State Builder

Students add multiple methods that change and report an object's state.

20-25 min Ages 9-15 OOP Builder JavaScript
Code Editor

Build energy actions.

Example code
class RobotPet {
  constructor(name) {
    this.name = name;
    this.energy = 2;
  }

  rest() {
    this.energy += 2;
    say(this.name + " rested. Energy: " + this.energy);
  }

  play() {
    this.energy -= 1;
    say(this.name + " played. Energy: " + this.energy);
  }

  status() {
    say(this.name + " has " + this.energy + " energy.");
  }
}

let bolt = new RobotPet("Bolt");
bolt.status();
bolt.play();
bolt.rest();
Output

Program result

Robot ready to speak your strings

Type real JavaScript, then run it.