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);

๊ฐ์ฒด ๋ฟŒ์‹œ๊ธฐ

  1. ์™ผ์ชฝ์— ๊ฐ์ฒด ๋ชจ์–‘์„ ๋งŒ๋“ ๋‹ค {}

  2. ์•ˆ์—๋Š” ์†์„ฑ ๊ฐ’์„ ์จ์ค€๋‹ค.

  3. ์˜ค๋ฅธ์ชฝ์—” ๊ฐ์ฒด ๋ณ€์ˆ˜๋ช…์„ ์จ์ค€๋‹ค.

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 ํ•˜์˜€๋‹ค. -> ๋งค๊ฐœ๋ณ€์ˆ˜

  1. ํ•จ์ˆ˜ ์ธ์ž๋ถ€๋ถ„์— ๋ณ€์ˆ˜๋ช…์„ ์ผ๋‹ค.

  2. ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ถ€๋ถ„์— {} ์“ฐ๊ณ  ์•ˆ์— ๋ถ€์‹œ๊ณ ์ž ํ•˜๋Š” 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);
  1. ํ•จ์ˆ˜์˜ ์ธ์ž๊ฐ’์œผ๋กœ ๋ฐฐ์—ด ๋ณ€์ˆ˜๋ช…์„ ์ ๋Š”๋‹ค.

  2. ๋งค๊ฐœ๋ณ€์ˆ˜์— []์„ ์ž‘์„ฑํ•˜๊ณ  ์•ˆ์— ์ง€์ •ํ•˜๊ณ ์ž ๋ถ€์‹œ๊ณ ์ž ํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ช…์„ ์ ๋Š”๋‹ค.

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?