- 프로퍼티 값 단축 구문(property -value -shorthand)
- ES6에서는 프로퍼티 값으로 변수를 사용하는 경우 변수 이름과 프로퍼티 키가 동일한 이름일 때 프로퍼티 키를 생략할 수 있다.
- 프로퍼티 키는 변수 이름으로 자동 생성 된다.
var id = 'p-0001';
var price = 30000;
// 기존 생성 방식
var product = {
id : id,
price : price
};
// ES6 단축 구문
var product2 = { id, price };
- 계산된 프로퍼티 이름(computed -property -name)
- ES6에서는 객체 리터럴 내부에서도 계산된 이름으로 프로퍼티 키를 동적으로 생성할 수 있다.
var prefix = 'key';
var index = 0;
var obj = {};
obj[prefix + '-' + index++] = index;
obj[prefix + '-' + index++] = index;
obj[prefix + '-' + index++] = index;
console.log(obj); //{ 'key-0': 1, 'key-1': 2, 'key-2': 3 }
- 메소드 단축(method -shorthand)
- ES6에서는 메소드를 정의할 때 function 키워드를 생략한 축약 표현을 사용 할 수 있음
var dog2 = {
name : '두부',
eat(food) { // function 생략
console.log(`${this.name}(은)는 ${food}를 맛있게 먹어요.`);
}
}