๐Ÿ“ƒ ํ”„๋ก ํŠธ์—”๋“œ ์ธํ„ฐ๋ทฐ ๋ฌธ์ œ ๋‹ตํ•ด๋ณด๊ธฐ #10 ํด๋กœ์ €๋Š” ๋ฌด์—‡์ด๋ฉฐ ์™œ ์‚ฌ์šฉํ•˜๋‚˜์š”? + Execution Context + Lexical Environment

Jake Seoยท2020๋…„ 6์›” 17์ผ
5

๐Ÿ“ƒ ํ”„๋ก ํŠธ์—”๋“œ ์ธํ„ฐ๋ทฐ ๋ฌธ์ œ ๋‹ตํ•ด๋ณด๊ธฐ #11 ํด๋กœ์ €๋Š” ๋ฌด์—‡์ด๋ฉฐ ์™œ ์‚ฌ์šฉํ•˜๋‚˜์š”?

Prologue

๊นƒํ—ˆ๋ธŒ๋ฅผ ๋Œ์•„๋‹ค๋‹ˆ๋‹ค๊ฐ€ ์˜ˆ์ „์— ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž ์ธํ„ฐ๋ทฐ ์งˆ๋ฌธ์ด๋ผ๋Š” ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๋ณธ์ ์ด ์žˆ๋‹ค. ์‚ฌ์‹ค ๊ทธ ๋‹น์‹œ์—๋Š” ๊ทธ๋ƒฅ ์Šคํƒ€๋งŒ ๋ˆŒ๋Ÿฌ๋†“๊ณ  ํฐ ๊ด€์‹ฌ์ด ์—†์—ˆ์ง€๋งŒ ์ง€๊ธˆ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ํ’€์–ด๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

๋ฌผ๋ก  ๋งŽ์€ ๋‹ต๋“ค์ด ์ด๋ฏธ ์ด ๋ ˆํฌ์ง€ํ† ๋ฆฌ์— ์ž‘์„ฑ๋˜์–ด ์žˆ๊ธด ํ•œ๋ฐ, ์ž‘์„ฑ๋˜์ง€ ์•Š์€ ๊ฒƒ๋“ค๋„ ์žˆ๊ณ  ๋‚ด ๋‹ต์ด๋ž‘ ๋ชจ๋ฒ”๋‹ต์•ˆ์„ ๋น„๊ตํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์„œ ํ•œ๋ฒˆ ํ’€์–ด๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ ์„ ์ •

๊ธฐ์กด ๋ ˆํฌ์ง€ํ† ๋ฆฌ์—๋Š” ๋ฌธ์ œ์˜ ๋ถ„๋ฅ˜๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

  1. General Questions
  2. HTML Questions
  3. CSS Questions
  4. JS Questions
  5. Accessibility Questions (external link)
  6. Testing Questions
  7. Performance Questions
  8. Network Questions
  9. Coding Questions
  10. Fun Questions

๋งค์ผ 1๊ฐ€์ง€์”ฉ ๋Œ์•„๊ฐ€๋ฉฐ ํ’€ ๊ฑด๋ฐ, General Questions ํ•ญ๋ชฉ์—์„œ ๋„ˆ๋ฌด ์ผ๋ฐ˜์ ์ธ ๋ฌธ์ œ๋Š” ํ’€์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. ๋„ˆ๋ฌด ์ผ๋ฐ˜์ ์ธ ๋ฌธ์ œ์˜ ์ •์˜๋Š” ์‚ฌ๋žŒ๋งˆ๋‹ค ๋‹ต์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์ด๋ฅผํ…Œ๋ฉด...

What did you learn yesterday/this week? (์–ด์ œ/์ด๋ฒˆ์ฃผ์— ๋ฌด์—‡์„ ๋ฐฐ์› ๋‚˜์š”)
What excites or interests you about coding? (๋ฌด์—‡์ด ๋‹น์‹ ์„ ์ฝ”๋”ฉํ•˜๊ณ  ์‹ถ๊ฒŒ ๋งŒ๋“œ๋‚˜์š”?)
What is a recent technical challenge you experienced and how did you solve it?(์ตœ๊ทผ์— ๊ฒช์€ ๊ธฐ์ˆ ์ ์ธ ์–ด๋ ค์›€๊ณผ ๊ทธ๊ฒƒ์„ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ–ˆ๋Š”์ง€ ์•Œ๋ ค์ฃผ์„ธ์š”)

์ด๋Ÿฌํ•œ ๊ฒƒ๋“ค์€ ๋‹ต๋ณ€ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ๋Š” ๊ทธ๋•Œ๊ทธ๋•Œ ๋‹ต์ด ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฐธ๊ณ 

๋‚˜์˜ ๋‹ต๋ณ€์€ ๋†’์€ ํ™•๋ฅ ๋กœ ํ‹€๋ฆด ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, ์•„๋‹ˆ๋ฉด ๋งž๊ฒŒ ๊ฐ€๋‹ค๊ฐ€ ์ค‘๊ฐ„์— ์ด์ƒํ•œ ๊ธธ๋กœ ๋น ์งˆ ์ˆ˜๋„ ์žˆ๋‹ค. ๋Œ“๊ธ€์— ์ž์‹ ๋งŒ์˜ ๋‹ต์„ ์˜ฌ๋ ค์ฃผ๋ฉด ๋งค์šฐ ๊ณ ๋งˆ์šธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Q: ํด๋กœ์ €๋Š” ๋ฌด์—‡์ด๋ฉฐ ์™œ ์‚ฌ์šฉํ•˜๋‚˜์š”?

What is a closure, and how/why would you use one?

A: ๋ ‰์‹œ์ปฌ ํ™˜๊ฒฝ๊ณผ ํ•จ์ˆ˜์˜ ์กฐํ•ฉ์ด๋ฉฐ, ๋ชจ๋“ˆํŒจํ„ด, ๋ถ€๋ถ„์ ์šฉ, ์ปค๋ง์— ์“ฐ์ž…๋‹ˆ๋‹ค.

ํด๋กœ์ €๋Š” ํ•จ์ˆ˜๊ฐ€ ์„ ์–ธ๋œ ๋ ‰์‹œ์ปฌ ํ™˜๊ฒฝ๊ณผ ํ•จ์ˆ˜์˜ ์กฐํ•ฉ์ž…๋‹ˆ๋‹ค. "๋ ‰์‹œ์ปฌ"์ด๋ž€ ๋‹จ์–ด๋Š” ๋ ‰์‹œ์ปฌ ์Šค์ฝ”ํ•‘์ด ์†Œ์Šค์ฝ”๋“œ ๋‚ด์—์„œ ๋ณ€์ˆ˜๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ณณ์„ ๊ฒฐ์ •ํ•˜๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜๊ฐ€ ์„ ์–ธ๋œ ์œ„์น˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค. ํด๋กœ์ €๋Š” ๋‚ด๋ถ€ ํ•จ์ˆ˜๋กœ ์™ธ๋ถ€ ํ•จ์ˆ˜์˜ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ํด๋กœ์ €๋Š” ์™ธ๋ถ€ ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜๋œ ํ›„์—๋„ ์™ธ๋ถ€ ํ•จ์ˆ˜์˜ ๋ณ€์ˆ˜ ๋ฒ”์œ„ ์ฒด์ธ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

๊ธฐ๋ณธ ์˜ˆ์ œ

์•„๋ž˜๋Š” MDN์— ์žˆ๋Š” Closure์˜ ์˜ˆ์ œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

function makeFunc() {
  var name = 'Mozilla';
  function displayName() {
    alert(name);
  }
  
  return displayName;
}

var myFunc = makeFunc();
myFunc();

์ผ๋ฐ˜์ ์œผ๋กœ ํ•จ์ˆ˜๊ฐ€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ ์ดํ›„์—๋Š” ๋‹ค์‹œ ๊ทธ ํ•จ์ˆ˜์˜ ๋‚ด๋ถ€ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ Closure๋ฅผ ๊ตฌ์„ฑํ•˜๋ฉด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํด๋กœ์ €๋Š” ๋งŒ๋“ค์–ด์ง„ ์‹œ์ ์—์„œ ํ™˜๊ฒฝ์— ์กด์žฌํ•˜๋Š” ์ง€์—ญ ๋ณ€์ˆ˜๋“ค์„ ๊ธฐ์–ตํ•ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ๋Š” makeFunc๊ฐ€ ๋™์ž‘ํ•  ๋•Œ, myFunc๋Š” displayName์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค. displayName์˜ ์ธ์Šคํ„ด์Šค๋Š” name ๋ณ€์ˆ˜๊ฐ€ ๋“ค์–ด์žˆ๋Š” ์ž์‹ ์˜ ๋ ‰์‹œ์ปฌ ํ™˜๊ฒฝ์„ ๊ธฐ์–ตํ•ฉ๋‹ˆ๋‹ค.

Function Factory ์˜ˆ์ œ

Function Factory๋ž€ ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ•จ์ˆ˜ ๊ณต์žฅ์ž…๋‹ˆ๋‹ค.

function makeAdder(x){
  return function(y) {
    return x + y;
  };
}

var add5 = makeAdder(5);
var add10 = makeAdder(10);

console.log(add5(2));
console.log(add10(2));

๋ชจ๋“ˆ ์˜ˆ์ œ

var myCounterModule = (function() {
  var counter = 0;
  return {
    increase: function() {
      alert(++counter);
    },
    decrease: function() {
      alert(--counter);
    },
    getCounter: function() {
      alert(counter);
      return counter;
    }
  }
})();

์œ„๋Š” ์ˆซ์ž๋ฅผ ์„ธ์–ด์ฃผ๋Š” ์นด์šดํ„ฐ์ž…๋‹ˆ๋‹ค. ๋ˆ„๊ตฌ๋„ counter๋ผ๋Š” ๋ณ€์ˆ˜์— ์ง์ ‘ ์ ‘๊ทผ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•˜์—ฌ ํ•ด๋‹น ๊ฐ’์„ ๋”ํ•˜๊ฑฐ๋‚˜ ๋บ„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์œ„์˜ ํŒจํ„ด์€ IIFE๋ผ๊ณ ๋„ ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค.

์ปค๋ง ์˜ˆ์ œ

์ปค๋ง์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ›์•„ ์ง„ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ฐ ํ•˜๋‚˜์˜ ์ธ์ž๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ํ•จ์ˆ˜๋“ค๋กœ ๋ณ€ํ˜•ํ•˜๋Š” ํ…Œํฌ๋‹‰์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

function addTwo(a, b) {
  return a + b;
}

addTwo(1, 2); // 3
function addTwo(a) {
  return function (b) {
    return a + b;
  };
}

addTwo(1)(2); // 3

๋‘ ์ฝ”๋“œ ์ค‘ ์œ„์˜ ๊ฒฝ์šฐ๊ฐ€ ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜์ด๊ณ  ์•„๋ž˜์˜ ๊ฒฝ์šฐ๊ฐ€ ์ปค๋ง์„ ์ ์šฉํ•œ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

'์ด ์ง“ํ•ด์„œ ์–ด๋”ฐ ์จ๋จน๋ƒ?' ํ•˜๋Š” ๋ถ„๋“ค์ด ์žˆ์„ ์ˆ˜ ์žˆ๋Š”๋ฐ ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ์กฐํ•˜๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค.
https://www.facebook.com/notes/kevin-lee/currying-%EC%96%B4%EB%94%B0-%EC%8D%A8%EB%A8%B9%EB%83%90%EA%B5%AC%EC%9A%94/214522735556858/

์œ„์˜ ๋งํฌ์—๋„ ๋‚˜์™€์žˆ์ง€๋งŒ,

  1. ํ•จ์ˆ˜์˜ Argument๋ฅผ ์œ ์—ฐ์„ฑ์žˆ๊ฒŒ ๋ฌดํ•œ ํ™•์žฅ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
function addInfiteArguments(a) {
    return function (b) {
        if(typeof b === 'number')
            return addTest(a + b);
        
        return a;
    }
}

addInfiteArguments(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(); // 55
  1. ์‹ค์ œ ํ•จ์ˆ˜์˜ ์‹คํ–‰์„ ๋งˆ์ง€๋ง‰ ์ •๋ณด๊ฐ€ ์ž…๋ ฅ๋  ๋•Œ๊นŒ์ง€ ๋ฌด๊ธฐํ•œ ๋’ค๋กœ ๋ฏธ๋ฃฐ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
profile
ํ’€์Šคํƒ ์›น๊ฐœ๋ฐœ์ž๋กœ ์ผํ•˜๊ณ  ์žˆ๋Š” Jake Seo์ž…๋‹ˆ๋‹ค. ์ฃผ๋กœ Jake Seo๋ผ๋Š” ๋‹‰๋„ค์ž„์„ ๋งŽ์ด ์”๋‹ˆ๋‹ค. ํ”„๋ก ํŠธ์—”๋“œ: Javascript, React ๋ฐฑ์—”๋“œ: Spring Framework์— ๊ด€์‹ฌ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

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