Byte's robot factory
Methods and this, classes and new, extends and super, getters and private fields.
const robot = {
name: "Bolt",
energy: 100,
hello() {
return "Beep! I am " + this.name;
},
};
console.log(robot.hello());
robot.name = "Nutty";
console.log(robot.hello());hello() { … } is the short way to write a method. Inside, this is the robot itself.this is the object the method was called on: the one before the dot in robot.hello(). Why not just write robot.name? Because the method should work for any robot, not one particular robot. this is the object's "me": "my name", "my charge".
We need hundreds of robots, and building each one as a literal is boring and risky: a bug in a method would have to be fixed in a hundred places. So we make a class — a blueprint. It has:
- a constructor — what to do with every new robot: write down its name, charge the battery;
- methods — what robots can do.
A new robot is built from the blueprint with the word new.
class Robot {
constructor(name) {
this.name = name;
this.energy = 100;
}
hello() {
return "Beep! I am " + this.name;
}
work(hours) {
this.energy -= hours * 10;
return this.energy;
}
}
const bolt = new Robot("Bolt");
const nut = new Robot("Nutty");
console.log(bolt.hello());
console.log(nut.hello());
bolt.work(3);
console.log(bolt.energy, nut.energy);
console.log(bolt.hello === nut.hello);What new Robot("Bolt") does:
- creates an empty object;
- links it to the prototype
Robot.prototype, where the class's methods live; - runs the constructor with this set to the new object;
- returns the finished robot.
That's why bolt.hello === nut.hello is true: the method isn't copied into every robot, it sits in the prototype once. An object created from a class is called an instance.
class Robot {
constructor(name) {
this.name = name;
this.energy = 100;
}
hello() {
return "Beep! I am " + this.name;
}
}
class Cleaner extends Robot {
constructor(name, room) {
super(name);
this.room = room;
}
hello() {
return super.hello() + ". Cleaning: " + this.room;
}
}
const vacuum = new Cleaner("Vacuumy", "the kitchen");
console.log(vacuum.hello());
console.log(vacuum.energy);
console.log(vacuum instanceof Cleaner, vacuum instanceof Robot);extends means "like Robot, but…". super(name) runs the parent's constructor, super.hello() its method.class Battery {
#charge = 100;
use(amount) {
this.#charge = Math.max(0, this.#charge - amount);
}
get status() {
return this.#charge > 20 ? "fine" : "running low";
}
}
const battery = new Battery();
console.log(battery.status);
battery.use(90);
console.log(battery.status);
console.log(battery.charge);get status() is a getter: read without parentheses, like a property. #charge is a private field: invisible and untouchable from outside.cart.add(...)), a game character (hero.attack()), UI components, API clients (new Stripe(key)). Even errors: in the errors chapter you'll meet new Error(...) — that's a class too, and you can inherit your own from it: class NotFoundError extends Error.Pop quiz
Mistakes are fine: keep trying until you find the right answer, and Byte explains why it's right.