비구조화 할당(Destructuring)
1
2
3
4
5
6
7
const testObject = {
name : '테스트',
price : 1000,
enname : 'test'
}
bad
1
2
3
4
let print( testObject ) {
return testObject.name + '이름 ' + testObject.price + '가격' + testObject.enname + '영문';
}
not bad
1
2
3
4
let print( testObject ) {
return `${testObject.name}이름 ${testObject.price}가격${testObject.enname}영문`;
}
good
1
2
3
let print = function( ${name,price,enname} ) {
return `${name}이름 ${price}가격 ${enname}영문`;
}
1
2
3
4
let print= function( testObject ) {
const ${name,price,enname} = testObject;
return `${name}이름 ${price}가격 ${enname}영문`;
}
템플릿 리터럴(Template literal)
1
2
3
4
5
6
7
const testObject = {
name : '테스트',
price : 1000,
enname : 'test'
}
bad
1
2
let print = testObject.name + '이름 ' + testObject.price + '가격' + testObject.enname + '영문';
good
1
2
let ${name,price,enname} = testObject;
let print = `${name}이름 ${price}가격 ${enname}영문`;
good(응용)
1
2
3
4
5
6
7
8
9
let print = function( name,price,enname ) {
let str0 = string[0];
let str1 = string[1];
let str2 = string[2];
return name + str0 + price + str1 + enname + str2 ;
}
let print2 = print`${name}이름 ${price}가격 ${enname}영문`;
spread 연산자 (Spread syntax)
1
2
3
4
5
6
7
const testObject = {
name : '테스트',
price : 1000,
enname : 'test'
}
중국어 이름을 추가해본다
bad
1
2
testObject['chname'] = Shíyàn;
not bad
1
2
Object.assign(testObject , {chname : Shíyàn});
good
1
2
testObject = {...testObject , chname : Shíyàn};
배열을 이용해 본다
good(array)
1
let testarray = ['test1','test2'];
bad
1
testarray.push('test3');
good
1
testarray = [...testarray, 'test3'];
반복문(Loops)
1
2
3
4
5
6
const testObject = [
{name : '테스트1', price : 1000, enname : 'test1' },
{name : '테스트2', price : 2000, enname : 'test2' },
{name : '테스트3', price : 3000, enname : 'test3' },
{name : '테스트4', price : 4000, enname : 'test4' }
]
bad
1
2
3
4
let totalprice = 0;
for(let i = 0 ; i < testObject.length ; i++ ) {
totalprice += testObject[i].price;
}
good
1
let totalprice = testObject.reduce((acc , cur) => acc + cur.price, 0);
async/await
1
let ran = () => { return Promise.resolve(Math.random())};
랜덤한 3개의 수의 합을 구해본다 Promise란
bad
1
2
3
4
5
6
7
let sum = () => {
let first;
let second;
let third;
return ran().then((num1) => {first = num1; return ran();})
.then((num2) => {second = num2; return ran();})
.then((num3) => {third = num3; return first + second + third;});
good
1
2
3
4
5
let sum = () => {
let first = await ran();
let second = await ran();
let third = await ran();
return first + second + third;});
Comments powered by Disqus.