method chaining

Donghun Seol·2023년 3월 31일
0

메서드가 this를 반환해주면 jQuery와 같이 메서드 체이닝을 활용할 수 있다.

class Calculator {
  constructor(public value: number = 0) {}
  add(value: number) {
    this.value += value;
    return this;
  }

  multiply(value: number) {
    this.value *= value;
    return this;
  }
}

const chainedCalc = new Calculator(1);

console.log(chainedCalc.add(1).multiply(10).add(-50).value);
profile
I'm going from failure to failure without losing enthusiasm

0개의 댓글