๐ ES6
๐ Shorthand property names
const name = 'haesa';
const age = '22';
// ๐ฉ
const haesa1 = {
name: name,
age: age,
};
// โจ
const haesa2 = {
name,
age,
};
์ค๋ธ์ ํธ๋ฅผ ๋ง๋ค ๋ key์ value์ ์ด๋ฆ์ด ๊ฐ๋ค๋ฉด ํ๋๋ก ์ถ์ฝํด์ ์์ฑํ ์ ์๋ค.
๐ Destructuring Assignment
Object
// object
const student = {
name: 'Anna',
level: 1,
};
// ๐ฉ
const name = student.name;
const level = student.level;
// โจ
const { name, level } = student;
const { name: studentName, level: studentLevel } = student;
์ค๋ธ์ ํธ์ ํ๋กํผํฐ๋ฅผ ๊ฐ๊ฐ ๋ณ์์ ํ ๋นํ ๋ ๋์ผํ ์ด๋ฆ์ key๋ฅผ ์ค๊ดํธ ์์ ๋ฃ๊ณ , ์ค๋ธ์ ํธ๋ฅผ ํ ๋นํด์ฃผ๋ฉด ๊ฐ key์ ๋ง๋ value๋ค์ด ๋ณ์์ ํ ๋น๋๋ค.
{}์์ ๋ณ์ ์ด๋ฆ์ด ์ค๋ธ์ ํธ์ key๋ก ์กด์ฌํ์ง ์๋๋ค๋ฉด undefine์ด ํ ๋น๋๋ค.
๋ง์ฝ key๊ฐ ์๋ ๋ค๋ฅธ ์ด๋ฆ์ผ๋ก ํ ๋นํ๊ณ ์ถ๋ค๋ฉด ์ฝ๋ก (:) ๋ค์ ๋ณ๊ฒฝํ๊ณ ์ ํ๋ ์ด๋ฆ์ ์์ฑํ๋ฉด ๋๋ค.
ex)
name์๋ name์ด๋ผ๋ key๊ฐ ๊ฐ์ง๊ณ ์๋ value๊ฐ, ์ฆ 'Anna'๊ฐ ํ ๋น๋๊ณ level์๋ 1์ด ํ ๋น๋๋ค.
Array
// array
const animals = ['๐ถ', '๐ฝ'];
// ๐ฉ
const first = animals[0];
const second = animals[1];
// โจ
const [first, second] = animals;
๋ฐฐ์ด๋ ๋ง์ฐฌ๊ฐ์ง๋ก ๋๊ดํธ ์์ ๋ณ์๋ฅผ ๋ฃ๊ณ ๋ฐฐ์ด์ ํ ๋นํด์ฃผ๋ฉด ๊ฐ ์ธ๋ฑ์ค ์์์ ๋ง๊ฒ ๋ฐฐ์ด์ ์์๋ค์ด ๋ณ์์ ํ ๋น๋๋ค.
ex) first์๋ animals[0]๊ฐ second์๋ animals[1]๊ฐ ํ ๋น๋๋ค.
๐ Spread Syntax
Object
// object copy
const obj1 = { key: 'key1' };
const obj2 = { key: 'key2' };
const obj3 = { ...obj1 };
// object merge
const dog1 = { dog: '๐' };
const dog2 = { dog: '๐ถ' };
const dog = { ...dog1, ...dog2 };
์ค๋ธ์ ํธ์ ๊ฐ๋ค์ ํ๋์ฉ ๊บผ๋ด์ ์๋ก์ด ์ค๋ธ์ ํธ๋ฅผ ์์ฑํ ์ ์๋ค.
๋ํ Spread Syntax๋ฅผ ์ฐ๋ฌ์ ์ฌ์ฉํ๋ฉด ์ฌ๋ฌ ์ค๋ธ์ ํธ์ ๋ณํฉ๋ ๊ฐ๋ฅํ๋ค.
์ด๋ key๊ฐ ๋์ผํ ์ค๋ธ์ ํธ๋ฅผ ๋ณํฉํ๊ฒ ๋๋ฉด ์ ์ผ ๋ค์ ์๋ ์์๊ฐ ์์ ์์๋ฅผ ๋ฎ์ด ์์ฐ๊ฒ ๋๋ค.
Array
// array copy
const array = [obj1, obj2];
const arrayCopy = [...array];
const arrayCopy2 = [...array, { key: 'key3' }];
// array concatenation
const fruits1 = ['๐', '๐'];
const fruits2 = ['๐', '๐ฅ'];
const fruits = [...fruits1, ...fruits2];
๋ฐฐ์ด์์ ์์๋ฅผ ํ๋์ฉ ๊บผ๋ด์ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค ์ ์๋ค.
์ด๋ ๋ฐฐ์ด์ ์์๊ฐ ์ค๋ธ์ ํธ์ธ ๊ฒฝ์ฐ ์ค๋ธ์ ํธ์ ์ฐธ์กฐ๊ฐ์ ๋ณต์ฌํด์จ๋ค.
์ค๋ธ์ ํธ์ ๋ง์ฐฌ๊ฐ์ง๋ก Spread Syntax๋ฅผ ์ฐ๋ฌ์ ์ฌ์ฉํ๋ฉด ๋ฐฐ์ด์ ๋ณํฉ๋ ๊ฐ๋ฅํ๋ค.
๐ Default parameters
// ๐ฉ
function printMessage(message) {
if (message == null) {
message = 'default message';
}
console.log(message);
}
printMessage('hello');
printMessage();
// โจ
function printMessage(message = 'default message') {
console.log(message);
}
printMessage('hello');
printMessage();
ํจ์์ ์ธ์๊ฐ์ด ์ฃผ์ด์ง์ง ์์์ ๋ ๊ธฐ๋ณธ๊ฐ์ด ํ ๋น๋๋ค.
๋ ์ ํํ ๋งํ์๋ฉด ์ ๋ฌ๋ ์ธ์์ ๊ฐ์ด undefined์ด๋ฉด ํ๋ผ๋ฏธํฐ์ default ๊ฐ์ด ํ ๋น๋๋ค.
๐ Ternary Operator
const isCat = true;
// ๐ฉ
let component;
if (isCat) {
component = '๐ธ';
} else {
component = '๐ถ';
}
// โจ
const component = isCat ? '๐ธ' : '๐ถ';
console.log(isCat ? '๐ธ' : '๐ถ');
์ฐธ๊ณผ ๊ฑฐ์ง์ ๋ฐ๋ผ ๊ฐ๊ธฐ ๋ค๋ฅธ ์ฐ์ฐ์ ์ํํ๊ณ ์ ํ ๋ ์ฌ์ฉํ ์ ์๋ค.
๐ Template Literals
const weather = '๐ค';
const temparature = '16°C';
// ๐ฉ
console.log('Today weather is ' + weather + ' and temparature is ' + temparature + '.');
// โจ
console.log(`Today weather is ${weather} and temparature is ${temparature}.`);
๋ฐฑํฑ์ ์ฌ์ฉํ๋ฉด ๋ฌธ์์ด๊ณผ ๋ณ์๋ฅผ ํ ๋ฒ์ ํํํ ์ ์๋ค.
๋ณ์๋ฅผ ์์ฑํ ๋๋ ${}์ ์ฌ์ฉํ๋ฉด ๋๋ค.
๐ ES11
๐ Optional Chaining
const person1 = {
name: 'Haesa',
job: {
title: 'S/W Engineer',
manager: {
name: 'Sara',
},
},
};
const person2 = {
name: 'Sara',
};
// ๐ฉ๐ฉ๐ฉ๐ฉ๐ฉ๐ฉ
function printManager(person) {
console.log(person.job.manager.name);
}
printManager(person1);
// printManager(person2);
person1์๋ job์ด ์์ง๋ง, person2์๋ job์ด ์๊ธฐ ๋๋ฌธ์ printManager์ person2๋ฅผ ์ ๋ฌํ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
์ ํจ์์ฒ๋ผ ํ๋กํผํฐ์ ์กฐ๊ฑด ์์ด ๊ทธ๋ฅ ์ ๊ทผํ๋ ๊ฒ์ ์ข์ง ์๋ค.
// ๐ฉ๐ฉ๐ฉ
function printManager(person) {
console.log(
person.job
? person.job.manager
? person.job.manager.name
: undefined
: undefined
);
}
printManager(person1);
printManager(person2);
๊ทธ๋ ๋ค๋ฉด ์์์ ์ ๋ฆฌํ Ternery Operator๋ฅผ ์ฌ์ฉํ๋ฉด ์ด๋จ๊น?
Ternery Operator๋ฅผ ์ค์ฒฉํด์ ์์ฑํ ์ ์๊ฒ ์ง๋ง ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง๋ค.
// ๐ฉ
function printManager(person) {
console.log(person.job && person.job.manager && person.job.manager.name);
}
printManager(person1);
printManager(person2);
์ด๋ฒ์ && ์ด์ฉํ๋ฉด ์ด๋จ๊น?
์๊น๋ณด๋ค ๊น๋ํด์ก์ง๋ง ์ค๋ณต๋๋ ์ฝ๋๋ค์ด ๋์ ๋ณด์ธ๋ค.
// โจ
function printManager(person) {
console.log(person.job?.manager?.name);
}
printManager(person1);
printManager(person2);
Optional Chaining์ ์ ์ฝ๋๋ค์ ๋ฌธ์ ์ ์ ํ ๋ฒ์ ํด๊ฒฐํด์ค๋ค.
person.job์ด ์์ผ๋ฉด manager์ ์ ๊ทผ, manager๊ฐ ์์ผ๋ฉด name์ ์ ๊ทผํด์ค๋ค.
๐ Nullish Coalescing Operator
// Logical OR operator
// falsy: false, '', 0, null, undefined
const name = null;
const userName = name || 'Guest';
console.log(userName);
๊ฐ์ด ๋ฌด์ธ๊ฐ ์ ๋๋ก ํ ๋น๋์ง ์์์ ๋๋ฅผ ๋๋นํด์ ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํด์ฃผ๊ธฐ ์ํด || Operator๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
// ๐ฉ
const name = '';
const userName = name || 'Guest';
console.log(userName);
const num = 0;
const message = num || 'undefined';
console.log(message);
๊ทธ๋ฐ๋ฐ ์์ฒ๋ผ ๋ณ์์ ''๊ณผ 0์ด ํ ๋น๋์์์๋ ๋ถ๊ตฌํ๊ณ ์ด๋ค์ false๋ก ํ๊ฐํด์ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ค์ ๋๋ ๊ฒฝ์ฐ๊ฐ ๋ฐ์ํ ์ ์๋ค.
// โจ
const name = '';
const userName = name ?? 'Guest';
console.log(userName);
const num = 0;
const message = num ?? 'undefined';
console.log(message);
Nullish Coalescing Operator๋ฅผ ์ฌ์ฉํ๋ฉด ์ ์์ ๋ค๋ณด๋ค ๋ ๋ช ํํ๊ฒ ์์ฑํ ์ ์๋ค.
?? operator ์์ ๋ฐ์ดํฐ๊ฐ null์ด๊ฑฐ๋ undefined๋ผ๋ฉด ?? operator ๋ค์ ์ฝ๋๊ฐ ์คํ๋๋ค.
ex)
name์ด ์๋ค๋ฉด name์ ์ฐ๊ณ , ์๋ค๋ฉด 'Guest'
num์ด ์๋ค๋ฉด num์ ์ฐ๊ณ , ์๋ค๋ฉด 'undefined'
'JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JS] ๋ถ๋ณ์ฑ๊ณผ ๊ฐ๋ณ์ฑ (0) | 2022.06.30 |
---|---|
[JS] Shallow Copy (1) | 2022.06.27 |
[JS] Getter์ Setter (0) | 2022.06.23 |
[JS] ํจ์์ ๋ฉ๋ชจ๋ฆฌ (0) | 2022.06.19 |