Function과 Arrow Function에서 this의 차이
3864 단어 JavaScriptJavaScript
Function
일반 function안의 this는 자신을 가장 마지막으로 품고 있는 scope로 항상 변하는 특징이 있다.
function callFunc(){
return {
foo : 25,
bar : function(){
console.log(this.foo);
}
}
}
callFunc.call({foo:100}).bar(); // 25
Arrow Function
Arrow Function은 익명함수 선언 방식으로 간결하게 코드를 작성할 수 있다.
보통 ( ) => { }의 모양이다. this와 arguments를 바인딩 하지 않는다.
function callFunc(){
return {
foo : 25,
bar : () => {
console.log(this.foo);
}
}
}
callFunc.call({foo:100}).bar(); // 100
화살표 함수 안에서의 this는 callFunc안의 this를 따르게 된다. 즉, 현재 환경의 this를 따르게 하고 싶을 때 화살표 함수를 사용하면 된다.
Author And Source
이 문제에 관하여(Function과 Arrow Function에서 this의 차이), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@shiningcastle/Function과-Arrow-Function에서-this의-차이저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)