Chapter 3: Digging to the Roots of JS - hochan222/Everything-in-JavaScript GitHub Wiki

Iteration

ES6๋Š” JS์˜ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ/ ์ปฌ๋ ‰์…˜ ํƒ€์ž…๋“ค์„ iterable๋“ค๋กœ ์ •์˜ํ–ˆ๋‹ค.

Closure

Closure๋Š” ํ•จ์ˆ˜๊ฐ€ ๊ธฐ์–ต์„ ํ•ด์„œ ๋ฒ”์œ„ ์™ธ๋ถ€์—์„œ ๋ณ€์ˆ˜์— ์•ก์„ธ์Šคํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

function greeting(msg) { 
  return function who(name) {
    console.log(`${ msg }, ${ name }!`); 
  };
}

var hello = greeting("Hello");
var howdy = greeting("Howdy"); 

hello("Kyle");
// Hello, Kyle!
hello("Sarah");
// Hello, Sarah!
howdy("Grant");
// Howdy, Grant!

์ผ๋ฐ˜์ ์œผ๋กœ ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด garbage collected ๋  ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค. (๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ œ๊ฑฐ ์˜๋ฏธ)
๊ทธ๋Ÿฌ๋‚˜ ๋‚ด๋ถ€ ํ•จ์ˆ˜ ์ธ์Šคํ„ด์Šค๋Š” ์—ฌ์ „ํžˆ ์‚ด์•„์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํด๋กœ์ €๋Š” msg ๋ณ€์ˆ˜๋ฅผ ์œ ์ง€ํ•œ๋‹ค.
ํด๋กœ์ €๋Š” msg ๋ณ€์ˆ˜ ๊ทธ ์ž์ฒด๋‹ค. ๋”ฐ๋กœ ๋ณธ๋œจ๊ฑฐ๋‚˜ ๋งŒ๋“  ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค. ์ด ๋ณ€์ˆ˜๋“ค์€ ์œ ์ง€๋˜๊ณ  ์—…๋ฐ์ดํŠธ๋„ ๋œ๋‹ค.

function counter(step = 1) { 
  var count = 0;
  return function increaseCount(){ 
    count = count + step; 
    return count;
  }; 
}

var incBy1 = counter(1); 
var incBy3 = counter(3);
incBy1(); //1 
incBy1(); //2

incBy3(); //3 
incBy3(); //6 
incBy3(); //9

ํด๋กœ์ €๋Š” ๋น„๋™๊ธฐ ์ฝ”๋“œ์—์„œ๋„ callback์œผ๋กœ ๋Œ€์ฒด๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

function getSomeData(url) { 
  ajax(url,function onResponse(resp){
    console.log(
    `Response (from ${ url }): ${ resp }`
    ); 
  });
}

getSomeData("https://some.url/wherever");
// Response (from https://some.url/wherever): ...

ํด๋กœ์ €๋Š” ๊ผญ ์™ธ๋ถ€ ๋ฒ”์œ„๊ฐ€ ํ•จ์ˆ˜์ผ ํ•„์š”๋Š” ์—†๋‹ค. ์ ์–ด๋„ ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜๊ฐ€ ์™ธ๋ถ€ ๋ฒ”์œ„์— ์žˆ์–ด๋„ ํด๋กœ์ €๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

this Keyword

ํ•จ์ˆ˜๊ฐ€ ์ •์˜๋˜๋ฉด ํด๋กœ์ €๋ฅผ ํ†ตํ•ด ๋‘˜๋Ÿฌ์‹ธ๋Š” Scope๊ฐ€ ๋งŒ๋“ค์–ด์ง„๋‹ค. Scope๋Š” ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ์–ดํ•˜๋Š” โ€‹โ€‹๊ทœ์น™ ์ง‘ํ•ฉ์ด๋‹ค.
ํ•จ์ˆ˜๋Š” ๊ทธ๋“ค์˜ Scope์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๋˜๋‹ค๋ฅธ ํŠน์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ด ํŠน์„ฑ์€ ์‹คํ–‰ ์ปจํ…์ŠคํŠธ(execution context)๋กœ ๊ฐ€์žฅ ์ž˜ ์„ค๋ช…๋˜๋ฉฐ this ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ํ•จ์ˆ˜์— ๋…ธ์ถœ๋œ๋‹ค.

์˜ค...! execution context ๋“œ๋””์–ด.. ๋“ค์–ด๋ณธ ๋‹จ์–ด๊ฐ€ ์ด๋ก ์— ๋“ฑ์žฅํ–ˆ๋”ฐ...

Scope๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ์ˆœ๊ฐ„ ์ •์ ์ด๊ณ  ๊ณ ์ •๋œ ๋ณ€์ˆ˜๋“ค์„ ํฌํ•จํ•˜๋Š”๋ฐ ํ•จ์ˆ˜์˜ execution context๋Š” ๋™์ ์ด๋ฉฐ ๊ทธ๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ๋ถˆ๋ฆฌ๋Š๋ƒ์— ๋”ฐ๋ผ์— ๋‹ฌ๋ผ์ง„๋‹ค. (ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ ๋  ๋•Œ๋งˆ๋‹ค ๋‹ฌ๋ผ์ง„๋‹ค.)
execution context๋Š” ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด์˜ ์†์„ฑ์ด๋‹ค.

function classroom(teacher) { 
  return function study() {
    console.log(
      `${ teacher } says to study ${ this.topic }`
    ); 
  };
}
var assignment = classroom("Kyle");

๋†€๋ž๊ฒŒ๋„ ์œ„ ์ฝ”๋“œ๋Š” ์˜ค๋ฅ˜๊ฐ€ ์•„๋‹ˆ๋‹ค. this๊ฐ€ execution context์— ์˜์กดํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

ํ•œํŽธ assignment();๋Š” undefined๊ฐ€ ๋‚˜์˜ค๋Š”๋ฐ ์ด๋Š” ์šฐ์„ , "strict" ๋ชจ๋“œ๋กœ ์„ค์ •ํ•˜์ง€ ์•Š์•˜๊ณ , ๊ทธ๋Ÿด ๊ฒฝ์šฐ ์ „์—ญ ๊ฐ์ฒด์˜(window)context๋กœ ๊ฐ€๋ฅดํ‚ค๊ฒŒ ๋ผ์„œ ์ „์—ญ ์ปจํ…์ŠคํŠธ์—๋Š” topic ๋ณ€์ˆ˜๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— undefined๊ฐ€ ๋‚˜์˜จ๋‹ค.

var otherHomework = { 
  topic: "Math"
};
assignment.call(otherHomework);

์šฐ๋ฆฌ๋Š” call ๋ฉ”์†Œ๋“œ๋กœ this์˜ ์ฐธ์กฐ๋ฅผ ํ•จ์ˆ˜ ํ˜ธ์ถœ์— ๋Œ€ํ•ด ์„ค์ • ํ•  ์ˆ˜ ์žˆ๋‹ค. (์˜ค์˜ค,, call์ด ์ด๋Ÿฐ ์˜๋ฏธ์˜€๋‹ค๋‹...!)

Prototypes

Object Linkage

this Revisited

โš ๏ธ **GitHub.com Fallback** โš ๏ธ