Skip to content

Latest commit

 

History

History
39 lines (35 loc) · 700 Bytes

File metadata and controls

39 lines (35 loc) · 700 Bytes

Рішення полягає у поверненні самого об’єкта з кожного виклику функції.

let ladder = {
  step: 0,
  up() {
    this.step++;
*!*
    return this;
*/!*
  },
  down() {
    this.step--;
*!*
    return this;
*/!*
  },
  showStep() {
    alert( this.step );
*!*
    return this;
*/!*
  }
};

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

Ми також можемо написати один виклик функції на кожну лінію коду. Для довгих ланцюгів коду це читабельніше:

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