Programming/Javascript

[ES6] Arrow Function / Spread operator

i독 2021. 10. 7. 04:19

Arrow Function => 문법은 주로 함수를 파라미터로 전달할 유용. this값이 다른 것이 특징이다.

일반함수는 자신이 종속된 객체 this 가리키지만 화살표 함수는 자신이 종속된 인스턴스 가리킨다.

 


function BlackDog(){
  this.name = 'White';
  return {
    name : 'Black';
    bark: function(){
      console.log(this.name + ' Bark!');
    }
  }
}
 
const blackDog = new BlackDog();
blackDog.bark(); // Black Bark!
function WhiteDog();
  this.name = 'White';
  return {
    name : 'Black';
    bark: () => {
      console.log(this.name + ' Bark!');
    }
  }
}
 
const whiteBog = new WhiteDog();
whiteDog.bark(); // White Bark!

Spread operator > 나열한 자료를 추출하거나 연결할 사용한다. 배열, 객체, 변수 앞에 마침표 개로 선언한다.

다만 배열, 객체, 함수 인자 표현식"[], {}, ()" 안에서만 사용된다.

var array1 = ['one', 'two'];
var array2 = ['three', 'four'];
var combined = [...array1, ...array2];
// combined = ['one', 'two', 'three', 'four'];
var [first, second, three = 'empty', ...others] = array1;
// first = 'one', second = 'two', three = 'empty', others = []