์๋ฐ์คํฌ๋ฆฝํธ ๋ฝ๊ฐ๊ธฐ
[1, 2, 3].filter(function(number)~์์ number๋ array์ ์์์ธ 1, 2, 3์ด ๋๋ค.ES6(์ฐธ๊ณ : poiemaweb)
๊ธฐ๋ณธ ๋งค๊ฐ ๋ณ์(๋งค๊ฐ๋ณ์ ๊ธฐ๋ณธ๊ฐ): ํจ์ ์ ์ ์ ๊ธฐ๋ณธ ๋งค๊ฐ๋ณ์์ ๊ธฐ๋ณธ๊ฐ์ ์ ์ธํ๋ฉด ํจ์ ๋ด์์ ์ธ์๋ฅผ ์ฒดํฌํ๊ฑฐ๋ ์ด๊ธฐํํ์ง ์์๋ ๋๊ณ , ์ด ๊ธฐ๋ณธ๊ฐ์ ๋งค๊ฐ๋ณ์์ ์ธ์๋ฅผ ์ ๋ฌํ์ง ์์์ ๋๋ง ์ ํจ
//b์๋ง ๋ํดํธ๊ฐ์ด ์๋ ๊ฒฝ์ฐ -> ๋ํดํธ๊ฐ์ด ์๋ ๋งค๊ฐ๋ณ์๋ ๋ค์ชฝ์
function multiply(a , b = 1) {
return a * b;
}
console.log(multiply(1)); // 1
console.log(multiply(1, 2)); // 2
์คํ๋ ๋(...): ๋์์ ๊ฐ๋ณ ์์๋ก ๋ถ๋ฆฌ ex.๋ฐฐ์ด...[1,2,3]์ 1, 2, 3์ผ๋ก, ๋ฌธ์์ด..."hello"๋ h, e, l, l, o๋ก ๋ถ๋ฆฌ
//ES5 push
const arr1 = ["h", "e", "l"];
const arr2 = ["l", "o"];
Array.prototype.push.apply(arr1, arr2);
console.log(arr1); // [ "h", "e", "l", "l", "o" ]
//ES6
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
// ...arr2๋ ["l", "o"]์ ๊ฐ๋ณ ์์๋ก ๋ถ๋ฆฌ
arr1.push(...arr2); // == arr1.push("l", "o");
console.log(arr1); // [ "h", "e", "l", "l", "o" ]
Rest ๋งค๊ฐ๋ณ์(...): ํจ์์ ์ ๋ฌ๋ ์ธ์๋ค์ ๋ชฉ๋ก์ ๋ฐฐ์ด๋ก ์ ๋ฌ๋ฐ์ -> ์คํ๋ ๋์ ๊ตฌ๋ถํ ๊ฒ
๋์คํธ๋ญ์ฒ๋ง(Destructuring): ๊ตฌ์กฐํ๋ ๋ฐฐ์ด(์์ ์๊ดO) ๋๋ ๊ฐ์ฒด(์์ ์๊ดX)๋ฅผ ๋น๊ตฌ์กฐํํด์ ๊ฐ๋ณ ๋ณ์์ ํ ๋น -> ๊ฐ์ฒด ๋น๊ตฌ์กฐํ๊ฐ ๋ ์์ฃผ ์ฐ์
//๋ฐฐ์ด ๋น๊ตฌ์กฐํ
const raceResults = ['MonstaX', 'MonstaY', 'MonstaZ'];
const [ gold, silver, bronze] = raceResults;
gold; //'MonstaX'
silver; //'MonstaY'
bronze; //'MonstaZ'
//rest ๋งค๊ฐ๋ณ์
const [ real, ...nothing ] = raceResults;
real; //'MonstaX'
nothing; //['MonstaY', 'MonstaZ']
//๊ฐ์ฒด ๋น๊ตฌ์กฐํ
const monstaX = {
leader: 'Shownu',
leadDancer: 'Hyungwon',
mainVocal: 'Kihyun'
}
const { leadDancer: bias } = monstaX; //const bias = monstaX.leadDancer;