ES6, ES11 Syntax Summary
2021. 7. 6. 18:57ใBig Dreamer_Developer/JS
/**
* 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,
age: age,
};
// โจ
const ellie3 = {
name,
age,
};
console.log(ellie1, ellie2, ellie3);
console.clear();
}
/**
* 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);
const { name: studentName, level: studentLevel } = student;
console.log(studentName, studentLevel);
}
// array
const animals = ['๐ถ', '๐ฝ'];
// ๐ฉ
{
const first = animals[0];
const second = animals[1];
console.log(first, second);
}
// โจ
{
const [first, second] = animals;
console.log(first, second);
}
console.clear();
}
/**
* 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];
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);
console.clear();
}
/**
* 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();
}
// โจ
{
function printMessage(message = 'default message') {
console.log(message);
}
printMessage('hello');
printMessage();
}
console.clear();
}
/**
* 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
* 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}.`);
}
/**
* ๊ด๋ จ ๊ฐ์ ์์: https://youtu.be/36HrZHzPeuY
*/
/**
* 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);
}
// ๐ฉ๐ฉ๐ฉ
{
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 (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';
console.log(userName);
const num = 0;
const message = num || 'undefined';
console.log(message);
}
// โจ
{
const name = '';
const userName = name ?? 'Guest';
console.log(userName);
const num = 0;
const message = num ?? 'undefined';
console.log(message);
}
}
'Big Dreamer_Developer > JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Arrays APIs (0) | 2021.07.07 |
---|---|
JavaScript Class & Callback function (0) | 2021.07.06 |
JavaScript async & await, Promise API (0) | 2021.07.06 |
JavaScript Promise (0) | 2021.07.06 |
Call Back Function (0) | 2021.07.06 |