[javascript] ES6의 화살표 함수

JUNE·2021년 3월 27일
0

javascript

목록 보기
2/2

function vs. () =>

  • function(일반 함수)은 자신이 종속된 '객체'를 this로 가리킨다.
  • ()=> 는 자신이 종속된 '인스턴스'를 가리킨다.
function BlackDog() {
    this.name = "white";
    return {
        name : 'black',
        bark : function(){
            console.log(this.name + ' dog : 멍멍'); 
            //black dog : 멍멍
        }
    }
}

function WhiteDog() {
    this.name = 'white';
    return {
        name : 'black',
        bark : () => {
            console.log(this.name + ' dog : 멍멍');
            //white dog : 멍멍
        }
    }
}

const blackDog = BlackDog();
const whiteDog = WhiteDog();

console.log("Black dog bark .... " )
blackDog.bark()

console.log("white dog bark ...." )
whiteDog.bark()

화살표 함수로 반환하기

function twice(value){
  return value*2;
}

//{}를 따로 열어 주지 않으면
//연산한 값을 그대로 반환한다는 의미
const triple = (value) => value * 3;

참고 : 리액트를 다루는 기술

profile
이것저것

0개의 댓글