const ๋ณ์๋ ํญ์ ์ฌํ ๋น ๊ธ์ง , ๋ฐฐ์ด ๋ํ ์ฌํ ๋น ๊ธ์งํ๋ ์๋ก์ด ์์๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ๊ฐ๋ฅ, ๊ฐ์ฒด ๋ํ ์ฌํ ๋น ๊ธ์งํ๋ ์๋ก์ด ์์ฑ์ ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ๊ฐ๋ฅ
const constNum = 0;
constNum = 0; //Uncaught TypeError: Assignment to constant variable. ์ฌํ ๋น๋์ด ์๋ฌ ๋ฐ์
const arr = []
const pushNum = 42;
arr.push(pushNum); //1 = arr.length
arr[0] = 42;
arr[1] = 3; // Uncaught TypeError: Assignment to constant variable. ์ฌํ ๋น๋์ด ์๋ฌ ๋ฐ์
const obj = { x:1 };
obj.x = 1;
delete obj.x;
obj = { x: 123 }; // Uncaught TypeError: Assignment to constant variable. ์ฌํ ๋น๋์ด ์๋ฌ ๋ฐ์
๐ค ์ฌํ ๋น๋ ์๋๋๋ฐ ์ const ํค์๋๋ฅผ ์ฐ๋๋?
https://google.github.io/styleguide/jsguide.html#features-use-const-and-let
๋ณ์์ ๊ฐ์ ์ฐพ์ ๋ ํ์ธํ๋ ๊ณณ
let funcExpressed = 'to be a function';
function funcDeclared(){
return 'this is a function declaration';
}
funcExpressed = function(){
return 'this is a function expression';
}
console.log(typeof funcDeclared);//'function'
console.log(typeof funcExpressed); //'string'
const funcContainer = { func : funcExpressed };
//๊ฐ์ฒด๋ฅผ ๋ณ์๋ก ์ ์ธํ์ฌ ๋ค์ ํ ๋ฒ funcExpressed ํจ์ ์คํ
console.log(funcContainer.func());
//'this is a function expression'
funcContainer.func = funcDeclared;
//๊ฐ์ฒด์ func ๊ฐ์ funcDelcared ํจ์๋ก ๋ณ๊ฒฝ
console.log(funcContainer.func());
// 'this is a function declaration'
ํธ์ด์คํ
let message = 'Outer'; // ์ ์ญ๋ณ์ Global
function getMessage() { return message; }
function shadowGlobal(){
let message = 'Inner'; // ์ง์ญ ๋ณ์
return message;
}
function shadowGlobal2(message){ return message; }
function shadowParameter(message){
message = 'Do not use parameters like this!;
return message;
}
console.log(getMessage()); // 'Outer' === message
console.log(shadowGlobal()); // ๋ณ์๋ช
์ด message๋ก ์ ์ญ, ์ง์ญ์ผ๋ก ๋์ผํ์ง๋ง ๋ด๋ถ ์ค์ฝํ์์๋ ์ง์ญ๋ณ์๊ฐ ๋ ์ฐ์ ์ด๋ฏ๋ก 'Inner' ์ถ๋ ฅ
console.log(shadowGlobal2('Parmeter')); // ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ ๊ฐ์ด ํธ์ถ 'Parameter'
console.log(shadowParameter('Parameter')); // ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ ๊ฐ์ ์ฌํ ๋นํ์ฌ ์ฌํ ๋นํ ๊ฐ์ ํธ์ถ 'Do not use parameters like this!'
console.log(message); //์ด๋ ํ ๋ด๋ถ ์ค์ฝํ๋ฅผ ๊ฑฐ์น์ง ์๊ณ ์ธ๋ถ ์ค์ฝํ์ ์ ์ญ ๋ณ์๋ก ์ถ๋ ฅ 'Outer'
function defaultParmeter(num = 5){ return num; }
console.log(defaultParameter()); // ๋งค๊ฐ๋ณ์๋ก ์ด๋ ํ ๊ฐ๋ ์ง์ ํ์ง ์์์ผ๋ฏ๋ก default ๊ฐ์ผ๋ก 5 ํธ์ถ
console.log(defaultParameter(10)); // ๋งค๊ฐ๋ณ์๋ก 10์ผ๋ก ์ง์ ํ์ผ๋ฏ๋ก default ๊ฐ์ด ์๋ ๋งค๊ฐ๋ณ์์ธ 10 ํธ์ถ
function pushNum(num, arr = []){//๋งค๊ฐ๋ณ์๋ก ์ซ์ํ ์ธ์์ ๋ฐฐ์ดํ ์ธ์
arr.push(num);
return arr;
}
console.log(pushNum(10));
//๋งค๊ฐ๋ณ์๋ก ์ซ์ํ ์ธ์๋ง ๋ค์ด์์ผ๋ default ๊ฐ์ผ๋ก ๋๋ฒ์งธ ์ธ์์ธ ๋ฐฐ์ด์ ๋น ๋ฐฐ์ด์ ๊ฐ๋ฆฌํจ๋ค. ๋ฐ๋ผ์ ๋น๋ฐฐ์ด์ 10 ์ push ํ์ฌ [10] ๋ฐํ
console.log(pushNum(20)); //[20] ๋ฐํ
console.log(pushNum(4,[1,2,3]);
//๋ชจ๋ ๋งค๊ฐ๋ณ์๊ฐ ๋ค ๋ค์ด์์ผ๋ฏ๋ก ํด๋น ๋ฐฐ์ด์ ์๋ฅผ pushํ๋ฉด [1,2,3,4] ๋ฐํ
function factories
namespacing private variables/functions
function increaseBy(increaseByAmount){
return function(numberToIncrease){//์ปค๋ง
return numberToIncrease + increaseByAmount;
}
}
const increaseBy3 = increaseBy(3);
const increaseBy5 = increaseBy(5);
//๋งค๊ฐ๋ณ์ increaseByAmount๋ฅผ ์๊ดํธ ์์ ์ซ์์ด๋ฉฐ ํจ์ ๋ด ์ฒซ๋ฒ์งธ ์ปค๋งํจ์๋ฅผ ๋ฐํํ์ง๋ง ๋งค๊ฐ๋ณ์์ธ numberToIncrease๋ ๋ฏธ์
console.log(increaseBy3(10)); // increaseByAmount = 3 , numberToIncrease = 10 => 3 + 10 = 13
console.log(increaseBy5(10)); // increaseByAmount = 5 , numberToIncrease = 10 => 5 + 10 = 15
//์์์ ์ง์ ํ ๋ณ์๋ค์๊ฒ ๋งค๊ฐ๋ณ์ numberToIncrease ๋ฅผ ์ง์ ํด์ค
console.log(increaseBy(8)(6) + increaseBy(5)(9)); // ๋ณ์๋ฅผ ์ ์ธํ์ง ์๊ณ ๋ ์ ์ฒด ํ๋ก์ธ์ค๋ฅผ ์งํ์์ผ ๊ฒฐ๊ณผ๊ฐ์ ๋ฐํํ ์ ์์ ( 8 + 6 ) + ( 5 + 9 ) = 14 + 14 = 28
let age = 27; //global
let name = 'jin'; //global
let height = 179; //global
function outerFn(){
let age = 24; //let ์ผ๋ก ์ฌ์ ์ธํ๊ธฐ ๋๋ฌธ์ global age ์๋ ๋ฌด๊ด
name = 'jimin'; //global ๋ณ์๋ฅผ ์ฌํ ๋น
function innerFn(){
age = 26; //innerFn ์ธ๋ถ์ด์ outerFn ๋ด๋ถ ์ค์ฝํ์ age ๋ฅผ ์ฌ ํ ๋น
let name = 'suga'; //let ์ผ๋ก ์ฌ์ ์ธํ๊ธฐ ๋๋ฌธ์ global name ๊ณผ๋ ๋ฌด๊ดํจ
return height;//global height๋ฅผ ๋ฐํ
}
innerFn();//outerFn ๋ด๋ถ ์ค์ฝํ InnerFn() ํจ์ ์คํ
console.log(age);//24
console.log(name); //jimin
return innerFn; //ํจ์ ์์ฒด๋ฅผ ๋ฐํ
}
const innerFn = outerFn(); //๋ด๋ถ ์ค์ฝํ outerFn() ํจ์ ํ ๋นํ์ฌ ํจ์ ์์ฒด๋ฅผ ๋ฐํ๊ฐ์ผ๋ก
console.log(age); //์ธ๋ถ ์ค์ฝํ์์ global ๋ณ์์ธ 27
console.log(name); //์ธ๋ถ ์ค์ฝํ์์ global ๋ณ์ ๊ฐ์ด outerFn์ด ์คํ๋๋ฉด์ ์ฌํ ๋น๋์๊ธฐ ๋๋ฌธ์ 'jimin'
console.log(innerFn()); //outerFn() -> return innerFn -> return height ์ด๋ฏ๋ก global ๋ณ์์ธ 179
const add = function(x, y) { return x + y; }
console.log(add(5,8)); //13
const add = (x, y) => { return x + y };
console.log(add(10,20)); //30
const subtract = (x, y) => x - y; //return ์๋ต ๊ฐ๋ฅ
console.log(subtract(10,20));//-10
const multiply = (x, y) => (x * y); //ํ์์ ๋ฐ๋ผ ์๊ดํธ
console.log(multiply(10,20));//200
const divideBy10 = x => x/10; //๋งค๊ฐ๋ณ์๊ฐ ํ๋์ผ ๊ฒฝ์ฐ ์๊ดํธ ์๋ต ๊ฐ๋ฅ
console.log(divideBy10(100));//10
const adder = x => {
return y => {
return x + y;
}
};
console.log(adder(50)(10)); //60
const subtractor = x => y => {//return ๋์ =>
return x - y;
}
console.log(subtractor(50)(10)); //40
const htmlMaker = tag => textContent => `<${tag}>${textContent}</${tag}>`
console.log(htmlMaker('div')('code states')); //<div>code states</div>
const liMaker = htmlMaker('li'); //๋งค๊ฐ๋ณ์ tag ๊ฐ li๋ก ์ ๋ฌ
console.log(liMaker('1st item')); // <li>1st item</li>
console.log(liMaker('2nd item')); // <li>2nd item</li>
let name = 'codestates'; //stringํ
console.log(name); // 'codestates'
console.log(name.toUpperCase()); //'CODESTATES'
console.log(name); // 'codestates'
name = name.toUpperCase();//์๋ก์ด ๊ฐ์ผ๋ก ์ฌํ ๋น
console.log(name); //'CODESTATES' ์ฌํ ๋นํ๊ธฐ ๋๋ฌธ์ ๊ฐ ๋ณ๊ฒฝ
let oeverTwenty = true; //booleanํ
let allowedToDrink = overTwenty; // ๊ฐ ์์ฒด ๋ณต์ฌ๊ฐ ๋์๊ธฐ ๋๋ฌธ์ allowedToDrink ๋ํ true
overTwenty = false; // ์๋ก์ด ๊ฐ์ผ๋ก ์ฌํ ๋น
console.log(overTwenty); //false
console.log(allowedToDrink);//true ๋ณต์ฌํ ๋ณ์๋ ๋ณต์ฌํ ๋์ ๊ทธ ๊ฐ์ ๊ทธ๋๋ก ์ ์ง
let variable = 'variable'; //string ํ
let variableCopy = 'variableCopy'; //string ํ
variableCopy = variable;// ๊ฐ์ ๋ณต์ฌํ๊ธฐ ๋๋ฌธ์ 'variable'
variable = variableCopy; // ๋ณต์ฌํ ๊ฐ์ ๋ค์ ๋ณต์ฌํ๊ธฐ ๋๋ฌธ์ 'variable'
console.log(variable === 'variable'); //true
let currentYear = 2020;
function afterTenYears(year){ year = year + 10; }
aterTenYears(currentYear); //ํจ์์คํ
console.log(currentYear); //2020 ์์ค์ ํจ์์คํํ์์ง๋ง ๋ฐํํ์ง๋ ์์๊ณ ๊ฐ ์์ฒด๋ง ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ๋์๊ธฐ ๋๋ฌธ์ ํฐ ์ํฅ์ ๋ผ์น์ง ์์
function afterTenYear(currentYear){//๋งค๊ฐ๋ณ์ currentYear๊ณผ ์ ์ญ ๋ณ์ currentYear ๋ ๋ณ์๋ช
์ด ๊ฐ์ง, ๋์ผํ ๋ณ์๋ ์๋๋ค
currentYear = currentYear + 10;
return currentYear;
}//๋งค๊ฐ๋ณ์๋ ํจ์์ ์ง์ญ๋ณ์๋ก ๋งค ํธ์ถ์ ๋ง๋ค ์๋กญ๊ฒ ์ ์ธ๋จ
let after10 = afterTenYear(currentYear);
console.log(currentYear); //2020 ๊ฐ์ ๋์ผ
console.log(after10);// ๋งค๊ฐ๋ณ์๋ก 2020์ ์ ๋ฌํ์ฌ 10์ ๋ํ ๊ฐ์ ํธ์ถํ 2030
์ฐธ์กฐ ์๋ฃํ์ ์ฃผ์๊ฐ ์ ์ฅ์ด ๋๋ฉฐ stack์๋ ์ฃผ์๊ฐ ์ ์ฅ์ด ๋์ด ์๊ณ heap์ ์ฃผ์๊ฐ ์ฐธ์กฐ๋ ๋ฐ์ดํฐ๊ฐ ์ ์ฅ์ด ๋จ
์ฐธ์กฐ ์๋ฃํ์ ๋ฐ์ดํฐ๋ ๋์ dynamic ์ผ๋ก ๋ณํจ
const arr = [1,2,3];
console.log(arr.length); //3
arr.push(4,5,6);
console.log(arr, arr.length); //[1,2,3,4,5,6] 6
arr.pop();
console.log(arr, arr.length); //[1,2,3,4,5] 5
const obj = {};
console.log(obj.length); //undefined;
console.log(Object.keys(obj).length); // 0
obj['name'] = 'codestates';
obj.quality = 'best';
obj.product = ['SW engineering', 'product manager', 'growth marketing', 'data science'];
console.log(Object.keys(obj).length); //3
delete obj.name;
console.log(Object.keys(obj).length); //2
const overTwenty = ['hongsik', 'minchul', 'hoyoung'];
const allowedToDrink = overTwenty;//์ฃผ์ ๋ณต์ฌ
overTwenty.push('san');
console.log(allowedToDrink); // ['hongsik', 'minchul', 'hoyoung', 'san']
overTwenty[1] = 'chanyoung';
console.log(allowedToDrink[1]); //'chanyoung'
// ๊ฐ์ ์ฃผ์๋ฅผ ์ฐธ์กฐํ๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ๋ณ๊ฒฝํ๋ฉด ์ํฅ์ ๋ฏธ์น๋ค.
const ages = [22,23,27];
allowedToDrink = ages; //๋ฐฐ์ด ์ฃผ์ ๋ณต์ฌํ์ฌ ์ฌํ ๋น
console.log(allowedToDrink === ages); //true ๋ฐฐ์ด ages ์ ๊ฐ์ ์ฃผ์ ์ฐธ์กฐ
consoel.log(allowedToDrink === [22,23,27]); //false ์์๋ค์ ๊ฐ๊ฒ ์ง๋ง ๋ค๋ฅธ ์ฃผ์๋ฅผ ์ฐธ์กฐ
const num1 = [1,2,3];
const num2 = [1,2,3];
console.log(num1 === num2);// false ๊ฐ์ ์์๋ค์ด์ง๋ง ๋ค๋ฅธ ์ฃผ์ ์ฐธ์กฐ
const person = { son: { age: 9}, };
const boy = person.son; // ๊ฐ์ฒด person์ ์์ฑ son ํค์ ๊ฐ์ธ {age:9}๋ฅผ ๋ณต์ฌํ์ฌ ํ ๋น
console.log(boy); // boy = { age : 9};
boy.age = 20; // boy = { age : 20 };
console.log(person.son.age === boy.age); // true 20 ๊ฐ์ ์ฃผ์ ์ฐธ์กฐ
consoel.log(person.son === { age: 9 }); // ์ฃผ์๊ฐ ๋ค๋ฆ
console.log(person.son === { age : 20}); //๊ฐ์ ๊ฐ์ง๋ง ์ฃผ์๊ฐ ๋ค๋ฆ
typeof arr = 'object' => 'array'๋ก ํ๋ณ ๋์ง ์์
Array.isArray(arr) = true => Array ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ true/false ๋ก ํ๋จ
arr.length === 0 => ๊ธธ์ด๊ฐ 0์์ ์ด์ฉ
const arr = ['peanut', 'butter', 'and', 'jelly'];
arr.slice(1); // 1 ~ ๋ ['butter', 'and', 'jelly']
arr.slice(0,1); // 0 ~ 0 ['peanut']
arr.slice(0,2); // 0 ~ 1 ['peanut', 'butter']
arr.slice(2,2); // 2 ~ 1 ์ด์๊ฐ์ง []
arr.slice(2,20); // 2 ~ ๋ ['and', 'jelly']
arr.slice(3,0); // 3 ~ -1 ์ด์ [] => end === 0
arr.slice(3,100); // 3 ~ ๋ ['jelly']
arr.slice(5,1);//5 > arr.length []
arr.slice(0); arr.slice(); //['peanut', 'butter', 'and', 'jelly']
๋ค๋ง, slice ๋ฉ์๋๋ ์ ๋ฐฐ์ด์ ์ฃผ์๋ฅผ ์ฐธ์กฐํ๋ ๊ฒ์ด ์๋ ๋ค๋ฅธ ์ฃผ์๋ฅผ ์ฐธ์กฐํ์ฌ ์๋ก์ด ๋ฐฐ์ด์ ์์ฑํ๋ ๊ฒ์ด๋ฏ๋ก ์๋ฐฐ์ด๊ณผ์ ๊ฐ์์ง๋ฅผ ๋น๊ตํ ๋๋ false๋ฅผ ๋ฐํํ๋ค.
arr.slice(0) === arr.slice(); //false ๋ ๋ค ์๋ก์ด ๋ฐฐ์ด์ ๋ฐํ์ํค๋๋ฐ ๊ฐ์ ์ฃผ์๊ฐ ์๋ **
let arr1 = [1,2,3];
let arr2 = arr1.slice(); //arr2 = [1,2,3]
console.log(arr1 === arr2); // false => ๊ฐ์ ์ฃผ์๋ฅผ ์ฐธ์กฐํ๋ ๊ฒ์ด ์๋๋ฏ๋ก false ๋ฐํ
const arr = ['zero', 'one', 'two', 'three', 'four', 'five'];
function passedByReference(refArr) {
refArr[1] = 'changed in function';
}
passedByReference(arr);
console.log(arr[1]);//'changed in function';
consoel.log(arr); // ['zero', 'changed in function', 'two', 'three', 'four', 'five'];
const assignedArr = arr; //๋ณต์ฌ = ๊ฐ์ ์ฃผ์ ์ฐธ์กฐ
assignedArr[5] = 'changed in assignedArr';
console.log(arr[5]);//'changed in assignedArr';
console.log(arr); //['zero', 'changed in function', 'two', 'three', 'four', 'changed in assignedArr'];
let obj = {};
typeof obj === 'object';
obj.length === undefined; //๋ฐ๋ณต๋๋ ์์ฑ์ ๊ฐ์ง์ง ์์๊ธฐ ๋๋ฌธ์ ๊ธธ์ด๋ฅผ ์์ ์์.
//๊ธธ์ด๋ฅผ ์๋ ค๋ฉด keys, values, entries ์์ฑ์ ์ด์ฉํ๋ค.
Object.keys(obj).length;
Object.values(obj).length;
Object.entries(obj).length;
๊ฐ์ฒด์ ์์ฑ
object.property = 'value'; , object['property'] = 'value'; ๋ฅผ ์ด์ฉํ์ฌ ๊ฐ์ ์ถ๊ฐํ ์ ์๋ค.
delete object.property; delete object['property']; ๋ฅผ ์ด์ฉํ์ฌ ํค-๊ฐ ์์ ์ญ์ ํ ์ ์๋ค.
'property' in object ๋ฅผ ์ด์ฉํ์ฌ ์์ฑ ํค๊ฐ ํด๋น ๊ฐ์ฒด์ ํฌํจ์ฌ๋ถ๋ฅผ ์ ์ ์๋ค.
์ ๋ฌ ์ธ์
const obj = { //์ฃผ์๋ ๊ฐ์ = ์ฐธ์กฐ์๋ฃํ
mastermind: 'Joker',
henchwoman: 'Harley',
relations: ['Anarky', 'Duela Dent', 'Lucy'],
twins: {
'Jared Leto': 'Suicide Squad',
'Joaquin Phoenix': 'Joker',
'Heath Ledger': 'The Dark Knight',
'Jack Nicholson': 'Tim Burton Batman',
},
};
function passedByReference(refObj) {
refObj.henchwoman = 'Adam West';
}
passedByReference(obj); //์์ ํจ์์ ์ํด 'henchwoman'์ ์์ฑ์ด 'Adam West'๋ก ๋ฐ๋
console.log(obj.henchwoman); // 'Adam West'
const assignedObj = obj; //๋ณต์ฌ = ์ฃผ์ ๊ฐ์ = ์๋ณธ ๊ฐ์ฒด๋ ๊ฐ์ด ๋ณ๊ฒฝ๋จ
assignedObj['relations'] = [1, 2, 3];
console.log(obj['relations']); // [1,2,3]
console.log(obj);
/*
const obj = {
mastermind: 'Joker',
henchwoman: 'Adam West',
relations: [1,2,3],
twins: {
'Jared Leto': 'Suicide Squad',
'Joaquin Phoenix': 'Joker',
'Heath Ledger': 'The Dark Knight',
'Jack Nicholson': 'Tim Burton Batman',
},
};
*/
ํ์ง๋ง, Object.assign(,)๋ฅผ ์ด์ฉํ ๋ณต์ฌ๋ ์ฃผ์๋ง ๋ณต์ฌํ๊ณ ๊ฐ๋ค์ ๋ณต์ฌํ์ง ์์์ ๊ฐ์ด ๋ณ๊ฒฝ๋์ด๋ ์๋ณธ ๊ฐ์ฒด์ ๊ฐ์ด ๋ค๋ฅผ ์ ์๋ค.
const copiedObj = Object.assign({}, obj); //๋น ๋ฐฐ์ด์ obj ๊ฐ์ฒด๋ฅผ ๋ณต์ฌํจ
copiedObj.mastermind = 'James Wood';
console.log(obj.mastermind);// 'Joker' ๋ณต์ฌํ ๊ฐ์ฒด์ ๊ฐ์ ๋ณ๊ฒฝํด๋ ์ ๊ฐ์ฒด์ ๊ฐ์ ๋ณ๊ฒฝ ๋์ง ์๊ฒ ๋จ
obj.henchwoman = 'Harley'; //์ ๊ฐ์ฒด์ ๊ฐ์ ๋ณ๊ฒฝํด๋ ๋ณต์ฌํ ๊ฐ์ฒด์ ๊ฐ์ ๋ณ๊ฒฝ๋์ง ์์
//์ฆ, ๋ณต์ฌํ ์์ ๋ง ๊ฐ๊ณ , ๊ทธ ํ์ ๊ฐ๊ฐ์ ๊ฐ์ฒด์ ๊ฐ์ ๋ณ๊ฒฝํด๋ ์๋ฌด๋ฐ ์๊ด์ด ์๊ฒ ๋๋ ๊ด๊ณ
delete obj.twins['Jared Leto'] ;//์ ๊ฐ์ฒด์ twins ๊ฐ์ฒด ์์ฑ์ 'Jared Leto' ์์ฑ์ ์ญ์ ๋จ
'Jared Leto' in copiedObj.twins; //false => ๊ทผ๋ฐ ์ด๊ฒ๋ ์ญ์ ๋จ ~~~~ ์ด์ด์์ด *** ๐จ
const spread = [1,2,3];
const arr = [0, spread, 4]; // [0,1,2,3,4] ํผ์ณ์
const spread = [];
const arr = [0, spread, 1]; // [0,1] ๋น ๋ฐฐ์ด์ผ ๊ฒฝ์ฐ ์๋ฌด๊ฒ๋ ์ถ๊ฐํ์ง ์์
const arr1 = [0,1,2], arr2 = [3,4,5];
const concatenated = [..arr1,...arr2]; //[0,1,2,3,4,5]
// const concatenated = [arr1,arr2];// ๋ฐฐ์ด๋ง ๋ฃ์ผ๋ฉด [[0,1,2],[3,4,5]]
arr1.concat(arr2); // [0,1,2,3,4,5]
const fullPre = {cohort: 7, duration: 4, mentor: 'hongsik', };
const me = { time: '0156', status: 'sleepy', todos: ['coplit', 'koans'], };
const merged = { ...fullPre, ...me }; //์์ฑ๋ง ์ ๋ฌ
// const merge = {fullPre, me} => { {cohort: 7, duration: 4, mentor: 'hongsik', } , { time: '0156', status: 'sleepy', todos: ['coplit', 'koans'], } }
//merge ์ ํ ๋น๋ ๊ฒ์ fullPre์ me์ reference ์ด๋ค. ์ด๋ shallow copy ์ธ๊ฐ deep copy???? **
console.log(merged.fullPre === fullPre); //true
console.log(merged.me === me); //true
function returnFirstArg(firstArg){ return firstArg; }
console.log(returnFirstArg('first', 'second', 'third')); // 'first' ํจ์ returnFirstArg ๋ ๋งค๊ฐ๋ณ์๊ฐ ํ๋์ด๊ธฐ ๋๋ฌธ์ ์ธ๊ฐ์ ๋งค๊ฐ๋ณ์๋ฅผ ์ ๋ฌํ์ฌ๋ ์ฒซ๋ฒ์งธ ์ธ์๋ง ์ ๋ฌ๋จ
function returnSecondArg(firstArg, secondArg){ return secondArg; }
console.log(returnSecondArg('only give first arg')); //undefined ํจ์ returnSecondArg ๋ ๋งค๊ฐ๋ณ์๊ฐ ๋๊ฐ๊ฐ ํ์ํ๊ณ ๊ทธ ์ค ๋๋ฒ์งธ ์ธ์๋ฅผ ๋ฐํํ๊ธฐ ๋๋ฌธ์ ์ ๋ฌ์ธ์๋ก ํ๋๋ง ์ฃผ์ด ๋๋ฒ์งธ ์ธ์๋ ์ ๋ฌ๋์ง ์์ ์์ ์์
function getAllParamsByRestParameter(...args){ return args; }
function getAllParamsByArgumentsObj(){ return arguments; }//arguments ๋ ๋ชจ๋ ํจ์์ ์คํ ์ ์๋์ผ๋ก ์์ฑ๋๋ ๊ฐ์ฒด
const restParms = getAllParamsByRestParameter('first', 'second', 'third');
const argumentsObj = getAllParamsByArgumentsObj('first', 'second', 'third');
consoel.log(restParms); //['first', 'second', 'third'] ์ ๋ฌ๋ฐ์ ์ธ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐฐ์ด๋ก ๋ง๋ค์ด ๋ฐํ
console.log(argumentsObj); //{ '0':'first', '1':'second', '2':'third' } ๋งค๊ฐ๋ณ์๋ก ๋ฐฐ์ด์ ๋ฐ์์ ๋ ๋ฐํํ๋ ๊ฒ์ ๊ฐ์ฒด์ธ๋ฐ ํค๋ ์ธ๋ฑ์ค๋ก ์ค์
console.log(Object.keys(argumentsObj); //['0','1','2'] ๊ฐ์ฒด์ ํค๋ง ๋ฐฐ์ด๋ก ๋ฐํ
console.log(Object.values(argumentsObj); //['first', 'second', 'third'] ๊ฐ์ฒด์ ๊ฐ๋ง ๋ฐฐ์ด๋ก ๋ฐํ
console.log(Object.entries(argumentsObj)); // [['0', 'first'], ['1','second'], ['2', 'third']]
console.log(restParms === argumentsObj);//false
console.log(restParms === Object.values(argumentsObj));//false ๊ฐ์ ๋ฐฐ์ด ์์๋ฅผ ๊ฐ์ง์ง๋ง ๋ค๋ฅธ ์ฃผ์ ์ฐธ์กฐ
console.log(typeof restParms, Array.isArray(restParms));//'object' true ์ด๋ ๋ฐฐ์ด
console.log(typeof argumentsObj, Array.isArray(argumentsObj));//'object' false ์ด๋ ๊ฐ์ฒด
function sum(...nums){
let sum = 0;
for(let i = 0 ; i < nums.length ; i++){
sum = sum + nums[i];
}
return sum;
}
console.log(sum(1,2,3));//6 ๋งค๊ฐ๋ณ์๋ก 1,2,3์ ...nums ์ ์ํด [1,2,3] ์ผ๋ก ๋ณํ๋์ด for ๋ฌธ ์งํ
function getAllParms(required1, required2, ...args){
return [required1, required2, arg];
}
console.log(getAllParms(123));
//[123, undefined, []) ์ธ ๊ฐ์ ๋งค๊ฐ๋ณ์๊ฐ ํ์ํ์ง๋ง
//ํ๋์ ์ ๋ฌ์ธ์๋ง ๋ฐ์์ ๊ฒฝ์ฐ ์ฒซ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ก ์ฐ๊ฒฐ์ํค๊ณ
//๋ฐ์ง ๋ชปํ ๋๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ์ undefined,
//์ธ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ ๋ฐฐ์ด๋ก ๋ฐ๋๋ฐ ์ด ๋ํ ์ ๋ฌ๋ฐ์ ์ธ์๊ฐ ์๊ธฐ ๋๋ฌธ์ ๋น๋ฐฐ์ด[] ๋ก ๋ฐํ
function makePizza(dough, name, ...toppings){
const order = `You ordered ${name} pizza with ${dough} dough and ${toppings.length} extra toppings!`;
return order;
}
console.log(makePizza('original'));
// dough = original , name = undefined , ...toppgins = []
// 'You ordered undefined pizza with original dough and 0 extra toppings!'
console.log(makePizza('thin', 'pepperoni'));
// dough = original , name = pepperoni , ...toppgins = []
// 'You ordered pepperoni pizza with original dough and 0 extra toppings!'
console.log(makePizza('napoli', 'meat', 'extra cheese', 'onion', 'bacon'));
// dough = napoli , name = meat , ...toppgins = [extra cheese, onion, bacon]
// 'You ordered meat pizza with napoli dough and 3 extra toppings!'
const array = ['code', 'states', 'im', 'course'];
const [first, second] = array; //['code', 'states']
console.log(first, second); //'code states'
const result = [];
function foo([first, second]){
result.push(second);
result.push(first);
}
foo(array);// ํจ์ foo ์ ๋งค๊ฐ๋ณ์ 2๊ฐ๋ array ๋ฐฐ์ด์ 0,1 ์ธ๋ฑ์ค ์์๋ง ์ ๋ฌ๋จ
console.log(result); //['state', 'code']์์ค์ ํจ์ ์คํ์ผ๋ก push๊ฐ ๋จ
const array = ['code', 'states', 'im', 'course'];
const [start, ...rest] = array;//['code', ['states', 'im', 'course']
console.log(start, rest); //'code ['state', 'im', 'course']
const [first, ...middle, last] = array; //๋ค์๊ณผ ๊ฐ์ด ์ค๊ฐ์ rest ๋ฌธ๋ฒ์ ์ฌ์ฉํ ์ ์๋ค.
const name = '๊น์ฝ๋ฉ';
const age = 28;
const person = { name, age, level: 'Junior', };
console.log(person);
/* person = {
name : '๊น์ฝ๋ฉ', age : 28, level: 'Junior'
}*/
const student = { name: '๋ฐํด์ปค', major : '๋ฌผ๋ฆฌํ๊ณผ' };
const { name } = student; //๊ฐ์ฒด student ์ name ํค๋ฅผ ๋ฐ๋ก ์ ์ฅ ๊ฐ๋ฅ = ๋ถํด
console.log(name); //'๋ฐํด์ปค'
const student = { name: '์ต์ด๋ณด', major: '๋ฌผ๋ฆฌํ๊ณผ' };
const {name, ...args} = student;
console.log(name); //'์ต์ด๋ณด'
console.log(args); //{major: '๋ฌผ๋ฆฌํ๊ณผ'}
const student = { name: '์ต์ด๋ณด', major: '๋ฌผ๋ฆฌํ๊ณผ', lesson: '์์์ญํ', grade: 'B+' };
function getSummary({name, lesson:course, grade}){
return `${name}๋์ ${grade}์ ์ฑ์ ์ผ๋ก ${course}์ ์๊ฐํ์ต๋๋ค`
}
console.log(getSummary(student));
// '์ต์ด๋ณด๋์ B+ ์ ์ฑ์ ์ผ๋ก ์์์ญํ์ ์๊ฐํ์ต๋๋ค.'
// ๋งค๊ฐ๋ณ์๋ก name, course, grade ๋ก ํ์ํ getSummary ํจ์์ธ๋ฐ, ๊ทธ ์ค lesson ์ student์ lesson๋ก ์ ์ฉ๊ฐ๋ฅํ๊ณ student์ major ํค๋ ํ์ํ์ง ์์
const user = {
name: '๊น์ฝ๋ฉ',
company: {
name: 'Code States',
department: 'Development',
role: {
name: 'Software Engineer'
}
},
age: 35
}
const changedUser = { ...user, name: '๋ฐํด์ปค', age: 20 } //user ๊ฐ์ฒด๋ฅผ ๋ณต์ฌํ๋, user์ name๊ณผ age์ ์์ฑ์ ์ฌํ ๋น
console.log(chagedUser);
/* changedUser = {
name: '๋ฐํด์ปค',
company: {
name: 'Code States',
department: 'Development',
role: {
name: 'Software Engineer'
}
},
age : 20
} */
const overwriteChanges = { name: '๋ฐํด์ปค', age: 20,...user}
// name ๊ณผ age ์ ์์ฑ์ ํ ๋นํ์ง๋ง, user ๊ฐ์ฒด๋ฅผ ๋ณต์ฌํ์ฌ user ์์ฑ์ค name๊ณผ age์ ๊ฐ์ผ๋ก ๋์ฒด๋์ด ๊ฒฐ๊ตญ user์ ๊ฐ์ ์ฃผ์๋ฅผ ์ฐธ์กฐ
console.log(overwriteChanges);
/* overwriteChanges = {
name: '๊น์ฝ๋ฉ',
company: {
name: 'Code States',
department: 'Development',
role: {
name: 'Software Engineer'
}
},
age: 35
} */
//overwriteChanges === users => true
const changedDepartment = {
...user,
company: {
...user.company,
department: 'Marketing'
}
}
//user ๊ฐ์ฒด๋ฅผ ๋ณต์ฌํ๋, user ์์ฑ ์ค company ํค์ ๊ฐ์ company ๊ฐ์ ์ถ๊ฐํ๊ณ department ๊ฐ์ ์ฌํ ๋น
console.log(changedDepartment);
/* changedDepartment = {
name: '๊น์ฝ๋ฉ',
company: {
name: 'Code States',
department: 'Marketing',
role: {
name: 'Software Engineer'
}
},
age: 35
} */