Develop/JavaScript
New Features in ES6
시크라기
2019. 3. 15. 08:40
Fat Arrow Function
const getNumber = () => 1;
const getArray = () => [1, 2, 3];
const getObject = () => ({a: 1, b: 2, c: 3});
const withParam = (payload) => payload;
Object Manipulation
const {a, b} = result;
const {data: {a, b}} = result;
const firstObj = {a, b}; (Same as {a: a, b:b})
const secondObj = {c, d};
const mergeObj = { ...firstObj, ...secondObj};
Async Function(Promise)
const isTrue = (payload) => {
return new Promise((resolve, reject) => {
if (payload === true) {
resolve(true);
} else {
reject(false);
}
});
};
isTrue(true).then((result) => {
console.log('is true');
}).catch((error) => {
console.log('is not true');
});
Module Exports
export const a = 1;
export const b = 2;
const c = 3;
const d = 4;
export {c, d};
export default defaultModule;
Module Import
import defaultModule from './defaultModule';
import { a, b } from './defaultModule';
Block Scope (let)
var num = 0;
for (let i = 0; i < 5; i++) {
num += i;
}
console.log(num); // 10
console.log(i); // undefined
Template Literal (`)
const a = 1;
const b = 'b';
const c = `a: ${a} and b: ${b}`;