let
λκ°μ λͺ
μΌλ‘ μ μν μ μλ€.
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
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);