let-const-rest-parameter-spread-operator-destructuring
let
์ฌ์ ์ธ ํ ์ ์๋ค.
block ์ค์ฝํ
๋๊ฐ์ ๋ช ์ผ๋ก ์ ์ํ ์ ์๋ค.
window์ ๋ถ์ง ์๋๋ค.
Quiz
for (var i = 1; i < 11 i++) {
setTimeout(function () {
console.log(i);
}, i * 1000);
}
for (let i = 1; i < 11 i++) {
setTimeout(function () {
console.log(i);
}, i * 1000);
}
Example
{
let jason = {
codename: 'blackbriar',
kill: function (target) {
target = null;
}
};
let operator = {
codename: 'onion',
answer: function () {
alert('run away!');
}
};
jason.kill(operator);
}
console.log(jason.codename);
console.log(operator.codename);
quiz
let jason = {
codename: 'blackbriar',
kill: function (target) {
target = null;
}
};
let operator = {
codename: 'onion',
answer: function () {
alert('run away!');
}
};
jason.kill(operator);
console.log(jason.codename);
ํ์ด
let jason = {
codename: 'blackbriar',
kill: function (target) {
var target = operator; //์ฐธ์กฐ๊ฐ์ด ๋ค์ด๊ฐ๋ค๊ณ ์๊ฐํ ์ ์๋ค. ์์น๊ฐ
target = null; //operator๋ ์๋ฌด ์ํฅ์ ๋ฐ์ง ์๋๋ค.
//๋งค๊ฐ๋ณ์๋ก ์ฃฝ์ด๋๊ฑด ๋ถ๊ฐ๋ฅํ๋ค.
//์ง์ ์ฃฝ์ด๊ณ ์ถ๋ค๋ฉด
//operator = null; ๋ก ํด์ผ ์ฃฝ์ผ ์ ์๋ค.
//target.codename = null ์ ๊ฐ๋ฅํ๋ค.'ใ
'/
}
};
let operator = {
codename: 'onion',
answer: function () {
alert('run away!');
}
};
jason.kill(operator);
console.log(jason.codename);
window์ ๋ถ์ง ์๋๋ค
var x = 'global';
let y = 'global';
console.log(window.x); //'global'
console.log(window.y); //undefined
why? window๊ฐ ๊ฐ์ฒด์ธ๋ฐ.. ์๋ ์์ฑํํ ์ ๊ทผํ๋ ค๊ณ ํด์์ด๋ค.
function doSomething() {
console.log(bar); // undefined
console.log(foo); // ReferenceError -
var bar = 1;
let foo = 2;
}
doSomething();
๋ ์๊ฒฉํด์ก๋ค.
VM307:3 Uncaught ReferenceError: Cannot access 'foo' before initialization
๋ผ๋ ์๋ฌ๋ฅผ ๋์ด๋ค. 'ใ ' hoisting์ด ์์ ์๋๋ค๊ณค ํ ์ ์์ผ๋ ๋ ์๊ฒฉํด์ ธ์ error๋ฅผ ๋์ด๋ค.
TDZ
Temporal dead zone(MDN) ์ฐธ์กฐ
hoisting์ด ์ผ์ด๋์ง๋ง var์๋ ๋ค๋ฅด๊ฒ var๋ undefined๋ก ์ด๊ธฐํ๊ฐ ๋๋๊ฑฐ์ง๋ง, let์ ์ด๊ธฐํ๊ฐ ์๋๋๊ฑฐ๋ค.
const
constant ์์(๋ณํ์ง ์๋ ์)์์ ์จ ์ด๋ฆ์ด๋ค.
์ฌ์ ์ธ ํ ์ ์๋ค.
์ฌํ ๋น ํ ์ ์๋ค.
Example
const obj = {
arr: [1, 2, 3]
};
obj = [];
obj.arr = null; //์์ ๋ด์ฉ๋ง ๋ฐ๊พธ๋๊ฑฐ๋ ์๊ด์๋ค.
obj.arr.length = 0 //?
obj.arr.push(1); //?
rest parameter
๋๋จธ์ง ๋งค๊ฐ๋ณ์
๋ฐฐ์ด๋ก ๋ง๋ค์ด ์ค๋ค.
function foo (a,b, ...c) {
console.log(c); // ['2', '3', '4', '5']
console.log(Array.isArray(c)); // true
}
foo('0', '1', '2', '3', '4', '5');
"arguments" is different
function foo2 (a, b, ...c) {
console.log(arguments); // Arguments [1, 2, 3, 4, 5]
console.log(Array.isArray(arguments)); //์ ์ฌ๋ฐฐ์ด false
}
foo2(1, 2, 3, 4, 5);
spread operator
๋ณ์์ ๊ฐ์ผ๋ก ์ฌ์ฉ๋ ์ ์๋ค.
์ธ์์ ์ฌ์ฉ ๋ ์ ์๋ค.
๋ฐฐ์ด๊ณผ ๊ฐ์ฒด์ [], {} ๋ชจ์์ ๋ฒ๊ธธ ์ ์๋ค.
Example
var arr1 = [1, 2, 3];
var arr2 = [4, 5, 6];
var total = [...arr1, ...arr2];
console.log(total); // [1, 2, 3, 4, 5, 6]
rest๋ ๋๊ฐ์ด ์๊ฒผ๋๋ฐ rest๋ ํจ์์์ ๋ง์ง๋ง ๋งค๊ฐ๋ณ์์์๋ง ์ฌ์ฉ๋๋ค.
Example
function foo (a, b, c) {
return a + b + c;
}
foo(...[1, 2, 3]); //6
Example
var agentA = {
codeName: 'oi',
powerLevel: -999
};
var agentAA = {
...agentA,
category: 'chaeso'
};
console.log(agentAA); //{codeName: 'oi', powerLevel: -999, category: 'chaeso'}
destructuring
๋ฐฐ์ด๊ณผ ๊ฐ์ฒด ๋ฟ์ ..
ํค๋ ๋ณ์๋ช ์ ์ธ ์๋ฆฌ์ ๋งค๊ฐ๋ณ์ ์๋ฆฌ์ ์ธ ์ ์๋ค.
๋ณ์๋ช ์ ๋ณ์์ ๊ฐ ์๋ฆฌ์ ์ธ์ ์๋ฆฌ์ ์ธ ์ ์๋ค.
๋ฐฐ์ด์ ์์์ ๊ฐ์ฒด์ ํค๊ฐ์ ํค ์ด๋ฆ(๋๋ ์๋ก์ด)์ ๋ณ์๋ก ๋ง๋ค ์ ์๋ค.
Example
var address = {
city: 'new york',
state: 'NY',
zipcode: '10003'
};
var { city, state } = address;
//์ค๋ฅธ์ชฝ์ ์๋ address ๊ฐ์ฒด๋ฅผ ํ๊ดดํ๋ค 'ใ
'/
//var city = address.city;
//var state = address.city; ๊ฐ ๋๋ค๊ณ ๋ณด๋ฉด ๋๋ค.
console.log(city + ', '+ state);
๊ฐ์ฒด ๋ฟ์๊ธฐ
์ผ์ชฝ์ ๊ฐ์ฒด ๋ชจ์์ ๋ง๋ ๋ค {}
์์๋ ์์ฑ ๊ฐ์ ์จ์ค๋ค.
์ค๋ฅธ์ชฝ์ ๊ฐ์ฒด ๋ณ์๋ช ์ ์จ์ค๋ค.
key๋ง๊ณ ๋ค๋ฅธ ๋ณ์ ๋ช
์ ์ฐ๊ณ ์ถ์ ๋
var address = {
city: 'new york',
state: 'NY',
zipcode: '10003'
};
var { city: c, state: s } = address;
// var c = address.city;
// var s = address.state;
console.log(c + ', ' + s);
๋งค๊ฐ๋ณ์์์๋ ๊ฐ์ฒด Destructuring
var address = {
city: 'new york',
state: 'NY',
zipcode: '10003'
};
function logAddress ({ city, state }) {
console.log(city + ', ' + state);
}
logAddress(address);
๋ณ์๋ฅผ ์ ์ธํ๊ณ ๋ง๋๋๊ณณ์ Destruturing ํ์๋ค. -> ๋งค๊ฐ๋ณ์
ํจ์ ์ธ์๋ถ๋ถ์ ๋ณ์๋ช ์ ์ผ๋ค.
๋งค๊ฐ๋ณ์ ๋ถ๋ถ์ {} ์ฐ๊ณ ์์ ๋ถ์๊ณ ์ ํ๋ key๋ฅผ ์ผ๋ค.
Array Destructuring
var numbers = [1, 2, 3, 4, 5];
var [ one, two, three, four, five ] = numbers;
console.log(one);
console.log(two);
Example
var numbers = [1, 2, 3, 4 ,5];
var [one, , , five ] = numbers;
console.log(one);
console.log(five);
๋งค๊ฐ๋ณ์์๋ ๋ฐฐ์ด Destructuring
var numbers = [ 1, 2, 3, 4, 5];
function sum ([ a, b, c, d, e]) {
console.log( a + b + c + d + e);
}
sum(numbers);
ํจ์์ ์ธ์๊ฐ์ผ๋ก ๋ฐฐ์ด ๋ณ์๋ช ์ ์ ๋๋ค.
๋งค๊ฐ๋ณ์์ []์ ์์ฑํ๊ณ ์์ ์ง์ ํ๊ณ ์ ๋ถ์๊ณ ์ ํ๋ ๋งค๊ฐ๋ณ์ ๋ช ์ ์ ๋๋ค.
rest parameter + destructuring
example 1
var numbers = [ 1, 2, 3, 4, 5 ];
function sum ([ a, b, ...c ]){
console.log(c);
}
sum(numbers); // [3, 4, 5]
example 2
const guicheokPeople = [1, 2, 3, 4 ,5 ];
function punish (...people) {
const [ a, b, c, d, e ] = people;
return c;
}
const result - punish(guicheokPeople);
ํ์ด
const guicheokPeople = [1, 2, 3, 4 ,5 ];
function punish (...people) {
//rest๋ ๋๋จธ์ง๋ฅผ ๋ฐฐ์ด๋ก ๋ฐํํด์ค๋ค.
//console.log(people); // [[1, 2, 3, 4, 5]] ๋ฐฐ์ด์ ๋ฐฐ์ด๋ก ๋ฐํ๋๋ค.
const [ a, b, c, d, e ] = people;
// const [ a, b, c, d, e ] = [[1, 2, 3, 4, 5]];
// a = [1, 2, 3, 4, 5 ];
return c;
}
const result = punish(guicheokPeople); //undefined
์ฒ์์ ์ํ๋ return c๋ฅผ ํ์ ๋ 3์ ๋์ถํ๋ ๋ฒ
๊ตฌ์กฐ๊ฐ ์ผ์นํด์ผ ํ๋ค
const guicheokPeople = [1, 2, 3, 4 ,5 ];
function punish (...people) {
const [ [a, b, c, d, e] ] = people;
return c;
}
const result = punish(guicheokPeople);
Last updated
Was this helpful?