๐Ÿฏ ์ฝ”์–ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ - 03 this

modoleeยท2020๋…„ 9์›” 13์ผ
2
post-thumbnail

๊ฐœ์š”

  • ๋‹ค๋ฅธ ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ์ฒด์ง€ํ–ฅ ์–ธ์–ด์—์„œ this๋Š” ํด๋ž˜์Šค๋กœ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด๋ฅผ ์˜๋ฏธ
  • ํด๋ž˜์Šค์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ˜ผ๋ž€์˜ ์—ฌ์ง€๊ฐ€ ์—†๊ฑฐ๋‚˜ ๋งŽ์ง€ ์•Š์Œ
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ this๋Š” ์–ด๋””์„œ๋“  ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ์ƒํ™ฉ์— ๋”ฐ๋ผ this๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” ๋Œ€์ƒ์ด ๋‹ฌ๋ผ์ง
  • ์–ด๋–ค ์ด์œ ๋กœ ๊ทธ๋ ‡๊ฒŒ ๋˜๋Š”์ง€ ํŒŒ์•…ํ•˜๊ธฐ ํž˜๋“  ๊ฒฝ์šฐ๋„ ์žˆ๊ณ , ์˜ˆ์ƒ๊ณผ ๋‹ค๋ฅด๊ฒŒ ์—‰๋šฑํ•œ ๋Œ€์ƒ์„ ๋ฐ”๋ผ๋ณด๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Œ

์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๋Š” this

  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ this๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๊ฐ€ ์ƒ์„ฑ ๋  ๋•Œ ํ•จ๊ป˜ ๊ฒฐ์ •. ์ฆ‰, ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ ๋  ๋•Œ ๊ฒฐ์ •

์ „์—ญ ๊ณต๊ฐ„์—์„œ์˜ this

์ „์—ญ ๊ณต๊ฐ„์—์„œ this๋Š” ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ด

  • ๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ : window
console.log(this);
console.log(window);
console.log(this === window);

  • Node.js ํ™˜๊ฒฝ : global
console.log(this);
console.log(global);
console.log(this === global);

์ „์—ญ ๊ณต๊ฐ„์—์„œ๋งŒ ๋ฐœ์ƒํ•˜๋Š” ํŠน์ดํ•œ ์„ฑ์งˆ

  • ์ „์—ญ ๋ณ€์ˆ˜ ์„ ์–ธ ์‹œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์ด ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ์ „์—ญ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋กœ๋„ ํ• ๋‹น
  • ์ „์—ญ ๋ณ€์ˆ˜ : ๋ณ€์ˆ˜ ์ด๋ฉด์„œ ๋™์‹œ์— ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ
var a = 1;
console.log(a); // ------ 1
console.log(window.a); // 1
console.log(this.a); // - 1

var ๊ฐ€ ์•„๋‹Œ let, const๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ 1 ์ด ์•„๋‹Œ undefined ๊ฐ’์ด ๋‚˜์˜ค๋Š”๋ฐ, ์ด๊ฑด ํ˜ธ์ด์ŠคํŒ…์ด ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ž„

  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ชจ๋“  ๋ณ€์ˆ˜๋Š” ํŠน์ • ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋กœ์„œ ๋™์ž‘
  • ํŠน์ • ๊ฐ์ฒด : ์‹คํ–‰ ์ปจํ…์ŠคํŠธ์˜ LexicalEnvironment
  • ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๊ฐ€ ๋ณ€์ˆ˜๋ฅผ ์ˆ˜์ง‘ํ•˜์—ฌ LexicalEnvironment์˜ ํ”„๋กœํผํ‹ฐ๋กœ ์ €์žฅ

์ „์—ญ ๋ณ€์ˆ˜์™€ ์ „์—ญ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ ํ• ๋‹น

  • ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ฒƒ๊ณผ ์ „์—ญ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ์— ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์ด ๋™์ผํ•˜๊ฒŒ ๋™์ž‘
var a = 1;
window.b = 2;
console.log(a, window.a, this.a); // 1 1 1
console.log(b, window.b, this.b); // 2 2 2

window.a = 3;
b = 4;
console.log(a, window.a, this.a); // 3 3 3
console.log(b, window.b, this.b); // 4 4 4

์ „์—ญ ๋ณ€์ˆ˜์™€ ์ „์—ญ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ ์‚ญ์ œ

  • ํ• ๋‹น๊ณผ ๋‹ค๋ฅด๊ฒŒ ์‚ญ์ œ๋Š” ์„œ๋กœ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘
  • ์ „์—ญ ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•œ ๊ฒฝ์šฐ : ์‚ญ์ œ ๋ถˆ๊ฐ€
  • ์ „์—ญ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋กœ ํ• ๋‹นํ•œ ๊ฒฝ์šฐ : ์‚ญ์ œ ๊ฐ€๋Šฅ
  • ์‚ฌ์šฉ์ž์— ์˜ํ•œ ์˜๋„์น˜ ์•Š์€ ์‚ญ์ œ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ์ „๋žต์œผ๋กœ ์ถ”์ธก
  • ์ „์—ญ ๋ณ€์ˆ˜ ์„ ์–ธ ์‹œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์ด ์ž๋™์œผ๋กœ ์ „์—ญ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋กœ ํ• ๋‹นํ•˜๋ฉฐ, ์ถ”๊ฐ€์ ์œผ๋กœ ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ์˜ ๋ณ€๊ฒฝ/์‚ญ์ œ ๊ฐ€๋Šฅ ์†์„ฑ์„ false๋กœ ์„ค์ •
var a = 1;
delete window.a; // ---------------- false
console.log(a, window.a, this.a); // 1 1 1

var b = 2;
delete b; // ----------------------- false
console.log(b, window.b, this.b); // 2 2 2

window.c = 3;
delete window.c; // ---------------- true
console.log(c, window.c, this.c); // ReferenceError: c is not defined

window.d = 4;
delete d; // ----------------------- true
console.log(d, window.d, this.d); // ReferenceError: d is not defined

๋ฉ”์„œ๋“œ๋กœ์„œ ํ˜ธ์ถœํ•  ๋•Œ ๊ทธ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ์˜ this

ํ•จ์ˆ˜ vs. ๋ฉ”์„œ๋“œ

  • ํ•จ์ˆ˜ : ๊ทธ ์ž์ฒด๋กœ ๋…๋ฆฝ์ ์ธ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰
  • ๋ฉ”์„œ๋“œ : ์ž์‹ ์„ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์— ๊ด€ํ•œ ๋™์ž‘์„ ์ˆ˜ํ–‰
  • ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ์— ํ• ๋‹น ๋œ ํ•จ์ˆ˜
    • ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋กœ์„œ ํ˜ธ์ถœ ํ•  ๊ฒฝ์šฐ : ๋ฉ”์„œ๋“œ๋กœ ๋™์ž‘. this๋Š” ํ• ๋‹น ๋œ ๊ฐ์ฒด
    • ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ : ํ•จ์ˆ˜๋กœ ๋™์ž‘. this๋Š” ์ „์—ญ ๊ฐ์ฒด
var func = function (x) {
  console.log(this, x);
};
func(1); // ---- Window { ... } 1

var obj = {
  method: func
};
obj.method(2); // {method: f} 2

  • ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ๊ณผ ๋ฉ”์„œ๋“œ๋กœ์„œ ํ˜ธ์ถœ ๊ตฌ๋ถ„ ๋ฐฉ๋ฒ•
    • ํ•จ์ˆ˜ ์ด๋ฆ„ ์•ž์— ์ (.)์ด ์žˆ๋Š” ๊ฒฝ์šฐ (์  ํ‘œ๊ธฐ๋ฒ•), ํ•จ์ˆ˜ ์ด๋ฆ„ ๋ฌธ์ž์—ด์ด ๋Œ€๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์—ฌ์ ธ ์žˆ๋Š” ๊ฒฝ์šฐ (๋Œ€๊ด„ํ˜ธ ํ‘œ๊ธฐ๋ฒ•) : ๋ฉ”์„œ๋“œ๋กœ์„œ ํ˜ธ์ถœ
    • ๊ทธ ์™ธ์˜ ๋ชจ๋“  ๊ฒฝ์šฐ : ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ
var obj = {
  method: function(x) { console.log(this, x); }
};
obj.method(1); // -- {method: f} 1
obj['method'](2); // {method: f} 2

๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ์˜ this

  • this์—๋Š” ํ˜ธ์ถœํ•œ ์ฃผ์ฒด์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋‹ด๊น€
  • ํ˜ธ์ถœํ•œ ์ฃผ์ฒด๋Š” ์  ํ‘œ๊ธฐ๋ฒ•, ๋Œ€๊ด„ํ˜ธ ํ‘œ๊ธฐ๋ฒ• ์•ž์— ์žˆ๋Š” ๊ฐ์ฒด
var obj = {
  methodA: function() { console.log(this); },
  inner: {
    methodB: function() { console.log(this); }
  }
};
obj.methodA(); // {methodB: f} ( === obj )
obj['methodA'](); // {methodB: f} ( === obj )

obj.inner.methodB(); // {methodB: f} ( === obj.inner )
obj.inner['methodB'](); // {methodB: f} ( === obj.inner )
obj['inner'].methodB(); // {methodB: f} ( === obj.inner )
obj['inner']['methodB'](); // {methodB: f} ( === obj.inner )

ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœํ•  ๋•Œ ๊ทธ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ์˜ this

ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ์˜ this

  • ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ ํ•  ๊ฒฝ์šฐ์—๋Š” ํ˜ธ์ถœ ์ฃผ์ฒด๊ฐ€ ๋ช…์‹œ ๋˜์ง€ ์•Š๊ณ  ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ๊ด€์—ฌํ•ด์„œ ์‹คํ–‰ํ•œ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— this๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์Œ
  • ์‹คํ–‰ ์ปจํ…์ŠคํŠธ ํ™œ์„ฑํ™” ํ•  ๋‹น์‹œ์— this๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ด
  • ๊ฒฐ๊ณผ์ ์œผ๋กœ ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ ํ•  ๊ฒฝ์šฐ this๋Š” ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ด
  • ๋”๊ธ€๋ผ์Šค ํฌ๋ฝํฌ๋“œ๋Š” ์ด๋ฅผ ๋ช…๋ฐฑํ•œ ์„ค๊ณ„ ์ƒ์˜ ์˜ค๋ฅ˜๋ผ๊ณ  ์ง€์ 

๋ฉ”์„œ๋“œ์˜ ๋‚ด๋ถ€ ํ•จ์ˆ˜์—์„œ์˜ this

  • ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœํ–ˆ๋Š”์ง€ ๋ฉ”์„œ๋“œ๋กœ์„œ ํ˜ธ์ถœํ–ˆ๋Š”์ง€๋งŒ ํŒŒ์•…ํ•˜๋ฉด this๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Œ
var obj1 = {
  outer: function () {
    console.log(this); // - obj1
    var innerFunc = function () {
      console.log(this); // Window, obj2
    }
    innerFunc();

    var obj2 = {
      innerMethod: innerFunc
    };
    obj2.innerMethod();
  }
};
obj1.outer();

๋ฉ”์„œ๋“œ์˜ ๋‚ด๋ถ€ ํ•จ์ˆ˜์—์„œ์˜ this๋ฅผ ์šฐํšŒํ•˜๋Š” ๋ฐฉ๋ฒ•

  • ํ˜ธ์ถœ ์ฃผ์ฒด๊ฐ€ ์—†์„ ๋•Œ๋Š” ์ž๋™์œผ๋กœ ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜์ง€ ์•Š๊ณ  ํ˜ธ์ถœ ๋‹น์‹œ ์ฃผ๋ณ€ ํ™˜๊ฒฝ์˜ this๋ฅผ ๊ทธ๋Œ€๋กœ ์ƒ์† ๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋ฉด ํ˜ผ๋ž€์ด ๋œ ํ•  ๊ฒƒ ๊ฐ™์Œ
  • self, this, that, ์ด๋ž€ ์ด๋ฆ„์˜ ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ์šฐํšŒ
  • ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅํ•ด์„œ ๋‚ด๋ถ€ ํ•จ์ˆ˜์—์„œ ํ™œ์šฉ
var obj = {
  outer: function () {
    console.log(this); // - obj
    var innerFunc1 = function () {
      console.log(this); // Window
    }
    innerFunc1();

    var self = this;
    var innerFunc2 = function () {
      console.log(self); // obj
    }
    innerFunc2();
  }
};
obj.outer();

this๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜์ง€ ์•Š๋Š” ํ•จ์ˆ˜

  • ES6์—์„œ this๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜์ง€ ์•Š๋Š” ํ™”์‚ดํ‘œ ํ•จ์ˆ˜(Arrow Function)๋ฅผ ์ƒˆ๋กญ๊ฒŒ ๋„์ž…
  • ์‹คํ–‰ ์ปจํ…์ŠคํŠธ ์ƒ์„ฑ ์‹œ this ๋ฐ”์ธ๋”ฉ ๊ณผ์ • ์ž์ฒด๊ฐ€ ๋น ์ง€๊ฒŒ ๋˜์–ด, ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ๊ทธ๋Œ€๋กœ ํ™œ์šฉ ๊ฐ€๋Šฅ
  • ๊ตณ์ด '์šฐํšŒ๋ฒ•'์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋จ
var obj = {
  outer: function () {
    console.log(this); // obj
    var innerFunc = () => {
      console.log(this); // obj
    };
    innerFunc();
  }
};
obj.outer();

์ฝœ๋ฐฑ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ๊ทธ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ์˜ this

  • ํ•จ์ˆ˜ A์˜ ์ œ์–ด๊ถŒ์„ ๋‹ค๋ฅธ ํ•จ์ˆ˜(๋˜๋Š” ๋ฉ”์„œ๋“œ) B์—๊ฒŒ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฒฝ์šฐ ํ•จ์ˆ˜ A๊ฐ€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜
  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋„ ํ•จ์ˆ˜์ด๊ธฐ์— ๊ธฐ๋ณธ์ ์œผ๋กœ this๊ฐ€ ์ „์—ญ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜์ง€๋งŒ, ์ œ์–ด๊ถŒ์„ ๋ฐ›์€ ํ•จ์ˆ˜์—์„œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์— ๋ณ„๋„๋กœ this๊ฐ€ ๋  ๋Œ€์ƒ์„ ์ง€์ •ํ•œ ๊ฒฝ์šฐ์—๋Š” ๊ทธ ๋Œ€์ƒ์„ ์ฐธ์กฐ
setTimeout(function () { console.log(this); }, 300); // Window

[1, 2, 3, 4, 5].forEach(function (x) {
  console.log(this, x); // Window 1, Window 2, Window 3, Window 4, Window 5
});

document.body.innerHTML += '<button id="a">ํด๋ฆญ</button>';
document.body.querySelector('#a')
  .addEventListener('click', function (e) {
    console.log(this, e); // button object
  });

  • ๊ฒฐ๊ณผ ํ™•์ธ
  • setTimeout ํ•จ์ˆ˜, forEach ๋ฉ”์„œ๋“œ๋Š” ๋‚ด๋ถ€์—์„œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ํ•  ๋•Œ ๋Œ€์ƒ์ด ๋  this๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์Œ
  • addEventListener ๋ฉ”์„œ๋“œ๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ํ•  ๋•Œ ์ž์‹ ์˜ this๋ฅผ ์ƒ์†ํ•˜๋„๋ก ์ •์˜ ๋˜์–ด ์žˆ์Œ
  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์ œ์–ด๊ถŒ์„ ๊ฐ€์ง€๋Š” ํ•จ์ˆ˜(๋ฉ”์„œ๋“œ)๊ฐ€ ์–ด๋–ค ์‹์œผ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š๋ƒ์— ๋”ฐ๋ผ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ this๋ฅผ ๊ฒฐ์ •

์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ์˜ this

  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ํ•จ์ˆ˜์— ์ƒ์„ฑ์ž๋กœ์„œ์˜ ์—ญํ• ์„ ํ•จ๊ป˜ ๋ถ€์—ฌ
  • new ๋ช…๋ น์–ด์™€ ํ•จ๊ป˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์ƒ์„ฑ์ž๋กœ์„œ ๋™์ž‘
  • ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ ๋œ ๊ฒฝ์šฐ ๋‚ด๋ถ€์—์„œ์˜ this๋Š” ๊ณง ์ƒˆ๋กœ ๋งŒ๋“ค ์ธ์Šคํ„ด์Šค ์ž์‹ ์„ ๊ฐ€๋ฆฌํ‚ด
  • ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ๊ณผ์ •
    • ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœ
    • ์ƒ์„ฑ์ž์˜ prototype ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฐธ์กฐํ•˜๋Š” __proto__ ๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๊ฐ€ ์žˆ๋Š” ๊ฐ์ฒด ์ƒ์„ฑ
    • ๋ฏธ๋ฆฌ ์ค€๋น„ ๋œ ๊ณตํ†ต ์†์„ฑ ๋ฐ ๊ฐœ์„ฑ์„ ํ•ด๋‹น ๊ฐ์ฒด(this)์— ๋ถ€์—ฌ
var Cat = function (name, age) {
  this.bark = '์•ผ์˜น';
  this.name = name;
  this.age = age;
};
var choco = new Cat('์ดˆ์ฝ”', 7);
var nabi = new Cat('๋‚˜๋น„', 5);
console.log(choco, nabi);

๋ช…์‹œ์ ์œผ๋กœ this๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ•

call ๋ฉ”์„œ๋“œ

Function.prototype.call(thisArg[, arg1[, arg2[, ...]]])
  • call ๋ฉ”์„œ๋“œ๋Š” call ๋ฉ”์„œ๋“œ์˜ ํ˜ธ์ถœ ์ฃผ์ฒด์ธ ํ•จ์ˆ˜๋ฅผ ์ฆ‰์‹œ ์‹คํ–‰ํ•˜๋„๋ก ํ•˜๋Š” ๋ช…๋ น
  • call ๋ฉ”์„œ๋“œ์˜ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋ฅผ this๋กœ ๋ฐ”์ธ๋”ฉ
  • ์ดํ›„ ์ธ์ž๋“ค์€ ํ˜ธ์ถœ ํ•  ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉ
  • ํ•จ์ˆ˜ ๊ทธ๋ƒฅ ์‹คํ–‰ ์‹œ this : ์ „์—ญ ๊ฐ์ฒด
  • call ๋ฉ”์„œ๋“œ๋กœ ์‹คํ–‰ ์‹œ this : ์ž„์˜๋กœ ์ง€์ •ํ•œ ๊ฐ์ฒด
var func = function (a, b, c) {
  console.log(this, a, b, c);
};

func(1, 2, 3); // -------------- Window { ... } 1 2 3
func.call({ x: 1 }, 4, 5, 6); // {x: 1} 4 5 6

  • ํ•จ์ˆ˜ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€
var obj = {
  a: 1,
  method: function (x, y) {
    console.log(this.a, x, y);
  }
};

obj.method(2, 3); // -------------- 1 2 3
obj.method.call({ a: 4 }, 5, 6); // 4 5 6

apply ๋ฉ”์„œ๋“œ

Function.prototype.apply(thisArg[, argsArray])
  • call ๋ฉ”์„œ๋“œ์™€ ๊ธฐ๋Šฅ์ ์œผ๋กœ ์™„์ „ํžˆ ๋™์ผ
  • call ๋ฉ”์„œ๋“œ์™€ ๋‹ค๋ฅธ ์ ์€ ํ˜ธ์ถœ ํ•  ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉ ํ•  ์ธ์ž๋“ค์„ ๋‚˜์—ดํ•˜์ง€ ์•Š๊ณ  ๋ฐฐ์—ด๋กœ ๋ฐ›๋Š”๋‹ค๋Š” ๊ฒƒ
var func = function (a, b, c) {
  console.log(this, a, b, c);
};
func.apply({x: 1}, [4, 5, 6]); // {x: 1} 4 5 6

var obj = {
  a: 1,
  method: function (x, y) {
    console.log(this.a, x, y);
  }
};
obj.method.apply({ a: 4 }, [5, 6]); // 4 5 6

call / apply ๋ฉ”์„œ๋“œ์˜ ํ™œ์šฉ

์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด์— ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋ฅผ ์ ์šฉ

var obj = {
  0: 'a',
  1: 'b',
  2: 'c',
  length: 3
};
Array.prototype.push.call(obj, 'd');
console.log(obj); // {0: "a", 1: "b", 2: "c", 3: "d", length: 4}

var arr = Array.prototype.slice.call(obj);
console.log(arr); // ["a", "b", "c", "d"]

  • ๊ฐ์ฒด์—๋Š” ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋ฅผ ์ง์ ‘ ์ ์šฉํ•  ์ˆ˜ ์—†์Œ
  • ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด : ํ‚ค๊ฐ€ 0 ๋˜๋Š” ์–‘์˜ ์ •์ˆ˜์ธ ํ”„๋กœํผํ‹ฐ๊ฐ€ ์กด์žฌํ•˜๊ณ  length ํ”„๋กœํผํ‹ฐ ๊ฐ’์ด 0 ๋˜๋Š” ์–‘์˜ ์ •์ˆ˜์ธ ๊ฐ์ฒด
  • ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด ์ธ ๊ฒฝ์šฐ call ๋˜๋Š” apply ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋ฅผ ์ฐจ์šฉ ๊ฐ€๋Šฅ
  • ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด์˜ ์˜ˆ
    • ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” arguments
    • querySelectAll, getElementsByClassName ๋“ฑ์˜ Node ์„ ํƒ์ž๋กœ ์„ ํƒํ•œ NodeList
function a () {
  var argv = Array.prototype.slice.call(arguments);
	argv.forEach(function (arg) {
    console.log(arg);
  });
}
a(1, 2, 3);

document.body.innerHTML = '<div>a</div><div>b</div><div>c</div>';
var nodeList = document.querySelectorAll('div');
var nodeArr = Array.prototype.slice.call(nodeList);
nodeArr.forEach(function (node) {
  console.log(node);
});

  • ์•ฝ๊ฐ„์˜ ์ œ์•ฝ์ด ์žˆ๋Š” ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด์˜ ์˜ˆ
    • ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์ธ๋ฑ์Šค์™€ length ํ”„๋กœํผํ‹ฐ๋ฅผ ์ง€๋‹ˆ๋Š” ๋ฌธ์ž์—ด
    • ๋ฌธ์ž์—ด์˜ ๊ฒฝ์šฐ length ํ”„๋กœํผํ‹ฐ๊ฐ€ ์ฝ๊ธฐ ์ „์šฉ
    • ์›๋ณธ ๋ฌธ์ž์—ด์— ๋ณ€๊ฒฝ์„ ๊ฐ€ํ•˜๋Š” ๋ฉ”์„œ๋“œ(push, pop, shift, unshift, splice ๋“ฑ)๋Š” ์—๋Ÿฌ ๋ฐœ์ƒ
    • ๋Œ€์ƒ์ด ๋ฐ˜๋“œ์‹œ ๋ฐฐ์—ด ์ด์–ด์•ผ ํ•˜๋Š” concat ๊ฐ™์€ ๊ฒฝ์šฐ ์—๋Ÿฌ๋Š” ์•ˆ ๋‚˜์ง€๋งŒ ์ œ๋Œ€๋กœ ๋œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์—†์Œ
var str = 'abc def';

// Array.prototype.push.call(str, ', pushed string');

var concatResult = Array.prototype.concat.call(str, 'string');
console.log(concatResult); // [String, "string"]

var everyResult = Array.prototype.every.call(str, function(char) { return char !== ' '; });
console.log(everyResult); // - false

var someResult = Array.prototype.some.call(str, function(char) { return char === ' '; });
console.log(someResult); // -- true

var newArr = Array.prototype.map.call(str, function(char) { return char + '!'; });
console.log(newArr); // ------ ["a!", "b!", "c!", " !", "d!", "e!", "f!"]

var newStr = Array.prototype.reduce.apply(str, [function(string, char, i) { return string + char + i; }, '']);
console.log(newStr); // ------ a0b1c2 3d4e5f6

  • call/apply ๋ฉ”์„œ๋“œ์˜ ๋ณธ๋ž˜ ์˜๋„์™€๋Š” ๋™ ๋–จ์–ด์ง„ ํ™œ์šฉ ๋ฒ•์œผ๋กœ ๊ฒฝํ—˜์„ ํ†ตํ•ด ์ˆจ์€ ๋œป์„ ์•Œ๊ณ  ์žˆ๋Š” ์‚ฌ๋žŒ์ด ์•„๋‹Œ ์ด์ƒ ์˜๋„ ํŒŒ์•…์ด ์‰ฝ์ง€ ์•Š์Œ
  • ES6์—์„œ ์ƒˆ๋กญ๊ฒŒ ๋„์ž… ๋œ Array.from
    : ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด ๋˜๋Š” ์ˆœํšŒ ๊ฐ€๋Šฅ ํ•œ ๋ชจ๋“  ์ข…๋ฅ˜์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋ฐฐ์—ด๋กœ ์ „ํ™˜
var obj = {
  0: 'a',
  1: 'b',
  2: 'c',
  length: 3
}
var arr = Array.from(obj);
console.log(arr); // ["a", "b", "c"]

์ƒ์„ฑ์ž ๋‚ด๋ถ€์—์„œ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž ํ˜ธ์ถœ

  • ์ƒ์„ฑ์ž ๋‚ด๋ถ€์— ๋‹ค๋ฅธ ์ƒ์„ฑ์ž์™€ ๊ณตํ†ต ๋œ ๋‚ด์šฉ์ด ์žˆ์„ ๊ฒฝ์šฐ call/apply ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ฝ”๋“œ ์ค‘๋ณต์„ ์ œ๊ฑฐ
function Person(name, gender) {
  this.name = name;
  this.gender = gender;
}

function Student(name, gender, school) {
  Person.call(this, name, gender);
  this.school = school;
}

function Employee(name, gender, company) {
  Person.apply(this, [name, gender]);
  this.company = company;
}

var yn = new Student('์˜ˆ๋‚˜', 'female', 'ํ•œ๊ตญ๋Œ€');
console.log(yn);
var sw = new Employee('์‹ ์šฑ', 'male', '์˜ค์ด๋งˆ์ผ“');
console.log(sw);

์—ฌ๋Ÿฌ ์ธ์ˆ˜๋ฅผ ๋ฌถ์–ด ํ•˜๋‚˜์˜ ๋ฐฐ์—ด๋กœ ์ „๋‹ฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ - apply ํ™œ์šฉ

var numbers = [10, 20, 3, 16, 45];
var max = min = numbers[0];
numbers.forEach(function(number) {
  if (number > max) {
    max = number;
  }
  if (number < min) {
    min = number;
  }
});

console.log(max, min); // 45 3
  • Math.max / Math.min ์— apply ์ ์šฉ
var numbers = [10, 20, 3, 16, 45];
var max = Math.max.apply(null, numbers);
var min = Math.min.apply(null, numbers);
console.log(max, min); // 45 3
  • ES6 spread operator ํ™œ์šฉ
const numbers = [10, 20, 3, 16, 45];
const max = Math.max(...numbers);
const min = Math.min(...numbers);
console.log(max, min); // 45 3

๋ณ„๋‹ค๋ฅธ ๋Œ€์•ˆ์ด ์—†์Œ

  • call/apply ๋ฉ”์„œ๋“œ๋Š” ๋ช…์‹œ์ ์œผ๋กœ ๋ณ„๋„์˜ this๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜๋ฉด์„œ ํ•จ์ˆ˜ ๋˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ํ›Œ๋ฅญํ•œ ๋ฐฉ๋ฒ•
  • ๊ทธ๋Ÿฌ๋‚˜ ์ด๋กœ ์ธํ•ด this๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์–ด๋ ต๊ฒŒ ๋งŒ๋“ค์–ด ํ•ด์„์„ ๋ฐฉํ•ด
  • ES5 ์ดํ•˜์—์„œ๋Š” ๋Œ€์•ˆ์ด ์—†์–ด ๋งค์šฐ ๊ด‘๋ฒ”์œ„ํ•˜๊ฒŒ ํ™œ์šฉ

bind ๋ฉ”์„œ๋“œ

Function.prototype.bind(thisArg[, arg1[,arg2[, ...]]])
  • ES5์—์„œ ์ถ”๊ฐ€ ๋œ ๋ฉ”์„œ๋“œ
  • call ๋ฉ”์„œ๋“œ์™€ ๋น„์Šทํ•˜์ง€๋งŒ ์ฆ‰์‹œ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ , this์™€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋„˜๊ฒจ ๋ฐ›์€ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
  • ์ƒˆ๋กœ์šด ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ๋˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋„˜๊ธฐ๋ฉด ๊ธฐ์กด์— ์ „๋‹ฌ ๋œ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋’ค์— ์ด์–ด์„œ ๋“ฑ๋ก
  • bind ๋ฉ”์„œ๋“œ์˜ ๋ชฉ์ 
    • ํ•จ์ˆ˜์— this๋ฅผ ๋ฏธ๋ฆฌ ์ ์šฉํ•˜๋Š” ๊ฒƒ
    • ๋ถ€๋ถ„ ์ ์šฉ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ
var func = function (a, b, c, d) {
  console.log(this, a, b, c, d);
}
func(1, 2, 3, 4); // --- Window { ... } 1 2 3 4

var bindFunc1 = func.bind({ x: 1 });
bindFunc1(5, 6, 7, 8); // {x: 1} 5 6 7 8

var bindFunc2 = func.bind({ x: 1 }, 4, 5);
bindFunc2(6, 7); // ----- {x: 1} 4 5 6 7
bindFunc2(8, 9); // ----- {x: 1} 4 5 8 9

name ํ”„๋กœํผํ‹ฐ

  • bind ๋ฉ”์„œ๋“œ๋ฅผ ์ ์šฉํ•ด์„œ ์ƒˆ๋กœ ๋งŒ๋“  ํ•จ์ˆ˜์˜ name ํ”„๋กœํผํ‹ฐ์—๋Š” bind์˜ ์ˆ˜๋™ํƒœ bound๊ฐ€ prefix๋กœ ๋ถ™์Œ
  • call/apply ๋ฉ”์„œ๋“œ ๋ณด๋‹ค ์ฝ”๋“œ ์ถ”์ ์ด ๋” ์ˆ˜์›”
var func = function (a, b, c, d) {
  console.log(this, a, b, c, d);
}

var bindFunc = func.bind({ x: 1 }, 4, 5);
console.log(func.name); // --- func
console.log(bindFunc.name); // bound func

์ƒ์œ„ ์ปจํ…์ŠคํŠธ์˜ this๋ฅผ ๋‚ด๋ถ€ ํ•จ์ˆ˜๋‚˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๊ธฐ

  • self ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•œ ์šฐํšŒ ๋ฐฉ๋ฒ• ๋Œ€์‹  call, apply ๋˜๋Š” bind๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ณด๋‹ค ๊น”๋”ํ•˜๊ฒŒ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ
var objWithCall = {
  outer: function () {
    console.log(this); // - objWithCall
    var innerFunc = function () {
      console.log(this); // objWithCall
    };
    innerFunc.call(this);
  }
};
objWithCall.outer();

var objWithBind = {
  outer: function () {
    console.log(this); // - objWithBind
    var innerFunc = function () {
      console.log(this); // objWithBind
    }.bind(this);
    innerFunc();
  }
};
objWithBind.outer();

  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” ํ•จ์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ ์ค‘ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด์—์„œ this๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, bind ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ์›ํ•˜๋Š” ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
var obj = {
  logThis: function () {
    console.log(this);
  },
  logThisLater1: function () {
    setTimeout(this.logThis, 500);
  },
  logThisLater2: function () {
    setTimeout(this.logThis.bind(this), 1000);
  }
};

obj.logThisLater1(); // Window { ... }
obj.logThisLater2(); // obj

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ ์˜ˆ์™ธ์‚ฌํ•ญ

  • ES6์—์„œ ์ถ”๊ฐ€ ๋œ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‹คํ–‰ ์ปจํ…์ŠคํŠธ ์ƒ์„ฑ ์‹œ this ๋ฐ”์ธ๋”ฉ์ด ์ œ์™ธ๋˜์–ด ํ•จ์ˆ˜ ๋‚ด๋ถ€์— this๊ฐ€ ์—†์œผ๋ฉฐ ๊ทธ๋กœ ์ธํ•ด ์Šค์ฝ”ํ”„ ์ฒด์ธ ์ƒ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด this์— ์ ‘๊ทผ
  • call/apply/bind ์ ์šฉ ํ•„์š” ์—†์ด ๋”์šฑ ๊ฐ„๊ฒฐํ•˜๊ณ  ํŽธ๋ฆฌ
var obj = {
  outer: function () {
    console.log(this); // - obj
    var innerFunc = () => {
      console.log(this); // obj
    };
    innerFunc();
  } 
};
obj.outer();

๋ณ„๋„์˜ ์ธ์ž๋กœ this๋ฅผ ๋ฐ›๋Š” ๊ฒฝ์šฐ(์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด์—์„œ์˜ this)

var report = {
  sum: 0,
  count: 0,
  add: function () {
    var args = Array.prototype.slice.call(arguments);
    args.forEach(function (entry) {
      this.sum += entry;
      ++this.count;
    }, this);
  },
  average: function () {
    return this.sum / this.count;
  }
};
report.add(60, 85, 95);
console.log(report.sum, report.count, report.average()); // 240 3 80

  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” ๋ฉ”์„œ๋“œ ์ค‘ ์ผ๋ถ€๋Š” ์ถ”๊ฐ€๋กœ this๋กœ ์ง€์ • ํ•  ๊ฐ์ฒด(thisArg)๋ฅผ ์ธ์ž๋กœ ์ง€์ • ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Œ
  • thisArg๋ฅผ ์ง€์ •ํ•˜๋ฉด ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this ๊ฐ’์„ ์›ํ•˜๋Š” ๋Œ€๋กœ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
  • ์—ฌ๋Ÿฌ ๋‚ด๋ถ€ ์š”์†Œ์— ๋Œ€ํ•ด ๊ฐ™์€ ๋™์ž‘์„ ๋ฐ˜๋ณต ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ์— ๋งŽ์ด ํฌ์ง„
    • Arrary์˜ forEach, map, filter, some, every, find, findIndex, flatMap, from
    • Set, Map (ES6์— ์ถ”๊ฐ€) ์˜ forEach
profile
๊ธฐ์ดˆ๊ฐ€ ํƒ„ํƒ„ํ•œ ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž๋ฅผ ๊ฟˆ๊ฟ‰๋‹ˆ๋‹ค.

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2020๋…„ 11์›” 28์ผ

์•ˆ๋…•ํ•˜์„ธ์š” ์ •๋ฆฌ ์ž˜ ํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ธ€ ๋‚ด์šฉ ์ค‘์— ์ž˜๋ชป๋œ ๋ถ€๋ถ„์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„ ๋Œ“๊ธ€ ๋‚จ๊น๋‹ˆ๋‹ค.
'์ „์—ญ ๊ณต๊ฐ„์—์„œ๋งŒ ๋ฐœ์ƒํ•˜๋Š” ํŠน์ดํ•œ ์„ฑ์งˆ' ๋ถ€๋ถ„์—์„œ
'var ๊ฐ€ ์•„๋‹Œ let, const๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ 1 ์ด ์•„๋‹Œ undefined ๊ฐ’์ด ๋‚˜์˜ค๋Š”๋ฐ, ์ด๊ฑด ํ˜ธ์ด์ŠคํŒ…์ด ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ž„' ์ด๋ผ๊ณ  ํ•˜์…จ๋Š”๋ฐ ์ด๊ฑด ํ˜ธ์ด์ŠคํŒ… ๋ฌธ์ œ๊ฐ€ ์•„๋‹ˆ๋ผ, let๊ณผ const ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ „์—ญ ๊ฐ์ฒด์— ํ”„๋กœํผํ‹ฐ๋กœ ํ• ๋‹น์„ ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  let๊ณผ const๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ๋„ ํ˜ธ์ด์ŠคํŒ…์€ ๋˜๋Š” ๊ฑธ๋กœ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
๋‹จ์ง€ var์™€ ๋‹ค๋ฅด๊ฒŒ ๋ณ€์ˆ˜ ์ƒ์„ฑ ์‹œ ์„ ์–ธ๊ณผ ์ดˆ๊ธฐํ™” ๋‹จ๊ณ„๊ฐ€ ๋ถ„๋ฆฌ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— console๋กœ ํ™•์ธํ–ˆ์„ ๋•Œ var์™€ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜จ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ ๋‚จ๊ฒจ๋“œ๋ ค์š”~!
https://curryyou.tistory.com/192 (#5. ์ „์—ญ ๊ฐ์ฒด ํ”„๋กœํผํ‹ฐ ์—ฌ๋ถ€)
https://medium.com/korbit-engineering/let%EA%B3%BC-const%EB%8A%94-%ED%98%B8%EC%9D%B4%EC%8A%A4%ED%8C%85-%EB%90%A0%EA%B9%8C-72fcf2fac365 (let, const์˜ ํ˜ธ์ด์ŠคํŒ… ์—ฌ๋ถ€)

1๊ฐœ์˜ ๋‹ต๊ธ€