en.javascript.info/1-js/04-object-basics/04-object-methods/8-chain-calls/task.md
Mau Di Bert 397a3aff4e
Update task.md
If it's not mentioned, the reader could not change code inside showStep() and then, showStep() could be just the last call of the statement.
2019-01-18 09:59:23 -03:00

622 B

importance: 2


Chaining

There's a ladder object that allows to go up and down:

let ladder = {
  step: 0,
  up() { 
    this.step++;
  },
  down() { 
    this.step--;
  },
  showStep: function() { // shows the current step
    alert( this.step );
  }
};

Now, if we need to make several calls in sequence, can do it like this:

ladder.up();
ladder.up();
ladder.down();
ladder.showStep(); // 1

Modify the code of up, down and showStep to make the calls chainable, like this:

ladder.up().up().down().showStep(); // 1

Such approach is widely used across JavaScript libraries.