
//Handler
class Rescuer {
constructor(code, next) {
this._code = code;
this._next = next;
}
//HandleRequest()
help(code) {
if (this._code === code) {
this.toHelp();
} else if (this._next != undefined) {
this._next.help(code);
}
}
}
//ConcreteHandler
class Firefighter extends Rescuer {
constructor(next) {
super(1, next);
}
toHelp() {
console.log("call firefighters");
}
}
//ConcreteHandler
class Police extends Rescuer {
constructor(next) {
super(2, next);
}
toHelp() {
console.log("call the police");
}
}
//ConcreteHandler
class Ambulance extends Rescuer {
constructor(next) {
super(3, next);
}
toHelp() {
console.log("call an ambulance");
}
}
let ambulance = new Ambulance();
let police = new Police(ambulance);
let firefighter = new Firefighter(police);
firefighter.help(1);
//printed: call firefighters
firefighter.help(2);
//printed: call the police
firefighter.help(3);
//printed: call an ambulance