importance: 2
There's a ladder
object that allows you to go up and down:
540d753e90789205fc6e75c502f68382c87dea9b
let ladder = {
step: 0,
up() {
this.step++;
},
down() {
this.step--;
},
showStep: function() { // показує поточний крок
alert( this.step );
}
};
Now, if we need to make several calls in sequence, we can do it like this:
540d753e90789205fc6e75c502f68382c87dea9b
ladder.up();
ladder.up();
ladder.down();
ladder.showStep(); // 1
ladder.down();
ladder.showStep(); // 0
Modify the code of up
, down
, and showStep
to make the calls chainable, like this:
540d753e90789205fc6e75c502f68382c87dea9b
ladder.up().up().down().showStep().down().showStep(); // shows 1 then 0
Such an approach is widely used across JavaScript libraries.
540d753e90789205fc6e75c502f68382c87dea9b