반응형
ES6 화살표 함수를 _ => {}
와 같이 사용하는 코드를 종종 목격하게 된다. 무슨 이유로 이렇게 사용할까?
화살표 함수에서 인자를 받지 않는 경우 () => {}
와 같이 사용한다. 하지만 _ => {}
로 쓰면 1글자가 더 적어진다. 즉, _
를 무시되는 매개변수로 사용하여 짧게 쓰려고 사용한다는 것이다.
이렇게 사용할 때에 유의해야할 점이 있는데,
_
를 매개변수로 선언해놓고 사용하지 않기 때문에, 린트 등으로 인해 Error/Warning이 발생할 수 있다.- Underscore/Lodash를 사용하면
_
를 사용하기 때문에 충돌이나서 해당 화살표 함수에서는 사용할 수 없을 수 있다.
참고 링크:
Using _ (underscore) variable with arrow functions in ES6/Typescript
Using _ (underscore) variable with arrow functions in ES6/Typescript
I came across this construct in an Angular example and I wonder why this is chosen: _ => console.log('Not using any parameters'); I understand that the variable _ means don't care/not used but...
stackoverflow.com
반응형