OOP Studio Ages 9-15
Object Builder
Students add properties and methods so each object can hold data and do actions.
Build two objects from one class.
Example code
class RobotPet {
constructor(name, color, energy) {
this.name = name;
this.color = color;
this.energy = energy;
}
play() {
this.energy -= 1;
say(this.name + " plays. Energy: " + this.energy);
}
describe() {
say(this.name + " is " + this.color);
}
}
let bolt = new RobotPet("Bolt", "blue", 5);
let zara = new RobotPet("Zara", "green", 4);
bolt.describe();
bolt.play();
zara.describe();
Program result
- Run your code to see output.
Type real JavaScript, then run it.
Object builder complete.
You made two objects from one class and gave them different property values.
Try OOP Challenge