Object Literal Syntax Extension
- 오브젝트의 key를 동적으로 움직일 수 있게끔 해주는 것
기존 오브젝트의 선언
var human = {
firstName: "DooHee",
lastName: "Kang",
};
console.log(human); // {firstName: 'DooHee', lastName: 'Kang'} 출력
변수를 선언하고 해당 변수를 value 값에 넣어 선언하기
이때 오브젝트의 key와 변수명은 같지 않다.
var firstName = "DooHee";
var lastName = "Kang";
var human2 = {
firstName: firstName,
lastName: lastName,
};
console.log(human2); // {firstName: 'DooHee', lastName: 'Kang'} 출력
Object Literal Syntax Extension은 key에 변수명을 넣음으로써 동적으로 움직이게 한다.
var type = "student";
var human2 = {
firstName: firstName,
lastName: lastName,
[type]: "대학생",
};
console.log(human2); // {firstName: 'DooHee', lastName: 'Kang', student: '대학생'} 출력
var school = "학교";
human2[school] = "00학교";
console.log(human2); // {firstName: 'DooHee', lastName: 'Kang', student: '대학생', 학교: '00학교'} 출력