자바스크립트의 혁명이라 할수 있는 ECMASript 2015(ES6) 이후 추가된 자바스크립트 최신 문법 중 자주 이용할 것 같은 기능들을 추려 정리해보는 시간이다.
2**3 //8
console.log(1_000_000_000 + 10_000); // 1000010000
console.log(1_00_00_00 + 10_0); // 1000100
const text = (...args) => console.log(args);
text`너의 정체가 도대체 뭐니?`;
// [["너의 정체가 도대체 뭐니?", raw: ['너의 정체가 도대체 뭐니']]]
const a = '정체가';
const b = '뭐니?';
test`너의 ${a} 도대체 ${b}`;
// [['너의 ', ' 도대체 ', ' ', raw: ['너의 ', ' 도대체 ', '']], '정체가', '뭐니?']
const brie1 = {
name: 'brie',
age: '18',
};
const name = 'brie';
const age = '18';
// 💩
const brie2 = {
name: name,
age: age,
};
// ✨
const brie3 = {
name,
age,
};
let room = {
number: 23,
name: "hotel",
toJSON() {
return 9999;
}
};
let meetup = {
title: "Conference",
room
};
// object
const student = {
name: 'Anna',
level: 1,
};
// 💩
const name = student.name;
const level = student.level;
console.log(name, level); // Anna 1
// ✨
const { name, level } = student;
console.log(name, level); // Anna 1
const { name: studentName, level: studentLevel } = student;
console.log(studentName, studentLevel); // Anna 1
// array
const animals = ['🐶', '😽'];
// 💩
const first = animals[0];
const second = animals[1];
console.log(first, second); // 🐶 😽
// ✨
const [first, second] = animals;
console.log(first, second); // 🐶 😽
const obj1 = { key: 'key1' };
const obj2 = { key: 'key2' };
const array = [obj1, obj2];
// array copy
const arrayCopy = [...array];
console.log(arrayCopy); // [ { key: 'key1' }, { key: 'key2' } ]
const arrayCopy2 = [...array, { key: 'key3' }];
obj1.key = 'newKey'; // array배열은 래퍼런스 값을 갖고있는 배열이다. 그래서 전개연산자로 복사하여도
// 레퍼런스 변수는 복사로 취급하지만, 그걸 잇는 주소연결은 똑같다.
console.log(array); // [ { key: 'newKey' }, { key: 'key2' } ]
console.log(arrayCopy2); // [ { key: 'newKey' }, { key: 'key2' }, { key: 'key3' } ]
// object copy
const obj3 = { ...obj1 };
console.log(obj3); // { key: 'newKey' }
// array concatenation
const fruits1 = ['🍑', '🍓'];
const fruits2 = ['🍌', '🥝'];
const fruits = [...fruits1, ...fruits2];
console.log(fruits); // [ '🍑', '🍓', '🍌', '🥝' ]
// object merge
const dog1 = { dog: '🐕' };
const dog2 = { dog: '🐶' };
const dog = { ...dog1, ...dog2 };
console.log(dog); // { dog: '🐶' }