ES6
Shorthand property names
/**
* Shorthand property names
* https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Object_initializer
*
*/
{
const ellie1 = {
name: 'Ellie',
age: '18',
};
const name = 'Ellie';
const age = '18';
// 💩
const ellie2 = {
name: name, //Ellie
age: age, //18
};
// ✨ 축약해서 코드 작성 가능
const ellie3 = {
name,
age,
};
console.log(ellie1, ellie2, ellie3);
console.clear();
}
Destructuring Assignment
/**
* Destructuring Assignment
* https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
*
*/
{
// object
const student = {
name: 'Anna',
level: 1,
};
// 💩
{
const name = student.name;
const level = student.level;
console.log(name, level);
}
// ✨
{
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; //0, 1번째에 접근 ( 강아지 고양이)
console.log(first, second);
}
console.clear();
}
Spread Syntax
/**
* Spread Syntax
* https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Spread_syntax
*
*/
{
const obj1 = { key: 'key1' };
const obj2 = { key: 'key2' };
const array = [obj1, obj2];
// array copy
const arrayCopy = [...array]; //🌟object의 참조값을 복사 받음
console.log(array, arrayCopy);
const arrayCopy2 = [...array, { key: 'key3' }];
obj1.key = 'newKey'; //🌟따라서 값을 바꾸면 전체가 다 바뀜(참조값을 복사 받아서)
console.log(array, arrayCopy, arrayCopy2);
// object copy
const obj3 = { ...obj1 };
console.log(obj3);
// 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); //🚨만약 키가 값은 object를 병합하면 뒤에꺼가 앞에꺼를 덮어씀
console.clear();
}
Default parameters
/**
* Default parameters
* https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Functions/Default_parameters
*/
{
// 💩
{
function printMessage(message) {
if (message == null) {
message = 'default message';
}
console.log(message);
}
printMessage('hello');
printMessage(); //undefined
}
// ✨
{
function printMessage(message = 'default message') { //초기값을 지정
console.log(message);
}
printMessage('hello');
printMessage();
}
console.clear();
}
Ternary Operator
/**
* Ternary Operator
* https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
*/
{
const isCat = true;
// 💩
{
let component;
if (isCat) {
component = '😸';
} else {
component = '🐶';
}
console.log(component);
}
// ✨
{
const component = isCat ? '😸' : '🐶';
console.log(component);
console.log(isCat ? '😸' : '🐶');
}
console.clear();
}
Template Literals
/**
* Template Literals
* https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/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
/**
* Optional Chaining (ES11)
* https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Optional_chaining
*/
{
const person1 = {
name: 'Ellie',
job: {
title: 'S/W Engineer',
manager: {
name: 'Bob',
},
},
};
const person2 = {
name: 'Bob',
};
// 💩💩💩💩💩💩
{
function printManager(person) {
console.log(person.job.manager.name);
}
printManager(person1);
// printManager(person2);
}
// 💩💩💩 Ternary Operator 사용
{
function printManager(person) {
console.log(
person.job
? person.job.manager
? person.job.manager.name
: undefined
: undefined
);
}
printManager(person1);
printManager(person2);
}
// 💩
{
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);
}
console.clear();
}
Nullish Coalescing Operator
/**
* Nullish Coalescing Operator (ES11)
* https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator
*/
{
// Logical OR operator
// false: false, '', 0, null, undefined
{
const name = 'Ellie';
const userName = name || 'Guest';
console.log(userName);
}
{
const name = null;
const userName = name || 'Guest';
console.log(userName);
}
// 💩
{
const name = '';
const userName = name || 'Guest'; //''도 false로 간주되서 Guest가 할당..이름을 안쓰고 싶은데..
console.log(userName); //Guest
const num = 0;
const message = num || 'undefined'; //undefined로 할당
console.log(message); //undefined
}
// ✨
{
const name = '';
const userName = name ?? 'Guest'; //이름이 있다면 name을 쓰고, 값이 없다면 Guest
console.log(userName); // 출력
const num = 0;
const message = num ?? 'undefined'; //num이 있다면 num쓰고, 없으면 undefined
console.log(message); //0
}
}
'Front-end > Web' 카테고리의 다른 글
[JS] 자바스크립트 프로처럼 쓰는 팁 (0) | 2021.08.31 |
---|---|
[JS] 자바스크립트 기초 4. 클래스 | 클래스 예제와 callback함수 정리 (0) | 2021.08.29 |
[JS] 자바스크립트 기초 3. 연산자 | boolean, &&연산자 (0) | 2021.08.29 |
[JS] 자바스크립트 기초 2. 함수 | 함수 정의, 호출, 그리고 callback (0) | 2021.08.28 |
[JS] 자바스크립트 기초1. 변수 | primitive 타입과 object의 차이점 (0) | 2021.08.28 |
댓글