ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

 

** โš  priv | ๊ฐ•์˜๋…ธํŠธ ์นดํ…Œ๊ณ ๋ฆฌ์— ์žˆ๋Š” ๋‚ด์šฉ์€ ๋ฐœํ–‰์ด ๋ชฉ์ ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ „ํ˜€ ์ •์ œ๋˜์–ด์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹น.**

 

์„น์…˜ 3:Javascript Foundation II

29. Execution Context

Creation Phase

  1. argument object created with any arguments
  2. initializes this keyword to point called or to the global object if not specified

Executing Phase

  1. Variable Environment created - memory space for variable and functions created
  2. initializes all variables to undefined and places them into memory with any new functions

 

35. Function Invocation

// Function Expression
var canada = () => {
  console.log('cold');
}

// Function Declaration
function india() {
  console.log('warm');
}

// Function Invocation/Call/Execution
canada();
india();

canada function (Function Expression) is defined at runtime
when we actually run/call/execute/invoke the function.

ใ„ด ํ•จ์ˆ˜ํ‘œํ˜„์‹์€ runtime ๋•Œ ์ •์˜๋จ.
๋ณ€์ˆ˜ ํ˜ธ์ด์ŠคํŒ…์ด ์ผ์–ด๋‚˜๋ฏ€๋กœ canada๋ผ๋Š” ๋ณ€์ˆ˜๊ฐ€ undefined๋กœ ์ƒ์„ฑ๋˜์—ˆ๋‹ค๊ฐ€, ํ•จ์ˆ˜๊ฐ€ ์‹ค์ œ๋กœ ์‹คํ–‰๋  ๋•Œ ๋‚ด์šฉ์ด ๋ณ€์ˆ˜์— ๋‹ด๊น€

india function (Function Declaration) ์€ ํ•จ์ˆ˜ ํ˜ธ์ด์ŠคํŒ…์ด ์ผ์–ด๋‚˜์„œ
ํ˜ธ์ด์ŠคํŒ…๋˜๋ฉด์„œ ๋‚ด์šฉ์ด ๋ฐ”๋กœ ์ •์˜๋จ

That is when the compiler initially looks at the code
and starts hoisting and allocating memory.

๊ธ€๊ณ  ์–˜๋Š” ์‹คํ–‰์ปจํ…์ŠคํŠธ๊ฐ€ ๋งŒ๋“ค์–ด์งˆ๋•Œ (๊ธ๊ป˜ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋Š” ์ˆœ๊ฐ„),
this ํ‚ค์›Œ๋“œ๋ž‘ arguments ๋„ ๊ฐ™์ด ๋งŒ๋“ค์–ด์ง

 

36. arguments Keyword

๊ธ€๋กœ๋ฒŒ ์ปจํ…์ŠคํŠธ๋ง๊ณ  ๊ฑ ํ•จ์ˆ˜ ์‹คํ–‰๋  ๋•Œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
๊ธ€๋กœ๋ฒŒ ์ปจํ…์ŠคํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ์—๋Ÿฌ๋‚จ (arguments is not defined)

The keyword arguments can be dangerous to use in your code as is. In ES6, a few methods were introduced that can help better use arguments. => ...arg

์–œ array๊ฐ€ ์•„๋‹ˆ๋ผ array like object์ž„

๊ทธ๋ž˜์„œ array ๋ฉ”์„œ๋“œ๋ชป์”€

var args = Array.from(arguments);
var args = [...arguments];    // ํŽผ์น˜๊ธฐ ์—ฐ์‚ฐ์ž, ์ „๊ฐœ ๊ตฌ๋ฌธ

 

Rest Parameters

function f(a, b, ...theArgs) {
  // ...
}
  • ํ•จ์ˆ˜์˜ ๋งˆ์ง€๋ง‰ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์•ž์— ... ๋ฅผ ๋ถ™์—ฌ (์‚ฌ์šฉ์ž๊ฐ€ ์ œ๊ณตํ•œ) ๋ชจ๋“  ๋‚˜๋จธ์ง€ ์ธ์ˆ˜๋ฅผ "ํ‘œ์ค€" ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐฐ์—ด๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ํŒŒ๋ผ๋ฏธํ„ฐ๋งŒ "Rest ํŒŒ๋ผ๋ฏธํ„ฐ" ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • rest ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” Array ์ธ์Šคํ„ด์Šค๋กœ, sort, map, forEach ๋˜๋Š” pop ๊ฐ™์€ ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ”๋กœ ์ธ์Šคํ„ด์Šค์— ์ ์šฉ๋  ์ˆ˜ ์žˆ์Œ์„ ๋œปํ•ฉ๋‹ˆ๋‹ค.

  • ๋‹จ, RestํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ํ•ญ์ƒ ์ œ์ผ ๋งˆ์ง€๋ง‰ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

  • ES6 ์˜ arrow function ์—์„œ๋Š” arguments ํ”„๋กœํผํ‹ฐ๊ฐ€ ์•„์˜ˆ ์—†์Œ
    ๋”ฐ๋ผ์„œ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ๊ฐ€๋ณ€ ์ธ์ž ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•  ๋•Œ๋Š” ๋ฐ˜๋“œ์‹œ rest ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

โ€‹ ++ spread ์—ฐ์‚ฐ์ž

function sandwich(a, b, c) {
  console.log(a); // '๐Ÿž'
  console.log(b); // '๐Ÿฅฌ'
  console.log(c); // '๐Ÿฅ“'
}

const food = ['๐Ÿž', '๐Ÿฅฌ', '๐Ÿฅ“'];

// Old way
sandwich.apply(null, food);

// โœ… ES6 way
sandwich(...food);

์•ž์—์„œ ์‚ดํŽด๋ณธ Rest ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” Spread ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ •์˜ํ•œ ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ํ˜•ํƒœ๊ฐ€ ๋™์ผํ•˜์—ฌ ํ˜ผ๋™ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

// spread ๋ฌธ๋ฒ•์— ์˜ํ•ด ๋ถ„๋ฆฌ๋œ ๋ฐฐ์—ด์˜ ์š”์†Œ๋Š” ๊ฐœ๋ณ„์ ์ธ ์ธ์ž๋กœ์„œ ๊ฐ๊ฐ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ „๋‹ฌ๋œ๋‹ค.
foo(1, ...[2, 3], 4, ...[5]);

https://poiemaweb.com/es6-extended-parameter-handling

 

37. Variable Environment

function one() {
  var isValid = true; // local env
  console.log("one: ", isValid);  // true
  two(); // new execution context!
}

function two() {
  var isValid; // undefined
  console.log("two: ", isValid);  // undefined
}

var isValid = false; // global
console.log("๊ธ€๋กœ๋ฒŒ: ", isValid); // false

one();

/*
   two() isValid = undefined
   one() isValid = true
   global() isValid = false
   ------------------------
   CALL STACK
*/

 

38. Scope Chain

39. [[scope]]

The execution context tells the engine
which lexical environment it is currently working in
and the lexical scope determines the available variables.

์Šค์ฝ”ํ”„์ฒด์ธ์„ ํ†ตํ•ด ๋ถ€๋ชจenv์˜ ๋ณ€์ˆ˜์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

Lexical Environment === [[scope]]

[[scopes]] ํ”„๋กœํผํ‹ฐ๋ฅผ ํ†ตํ•ด์„œ ์Šค์ฝ”ํ”„์ฒด์ธํ™•์ธ ๊ฐ€๋Šฅ

 

40. Exercise: JS is Weird

function weird() {
  height = 50;    // ๐Ÿค”
  return height;
}

weird();

2๋ฒˆ์งธ๐Ÿค”์ค„์—์„œ ์ผ์–ด๋‚˜๋Š”์ผ
weird :ใ…‡ใ…ใ…‡? our variable env์—๋Š” height๊ฐ€ ์—†์–ด..
๊ทธ๋Ÿฌ๊ณ  ๊ธ€๋กœ๋ฒŒ env๋กœ ๊ฐ€์„œ ๋ฌผ์–ด๋ด„
๊ธ€๋กœ๋ฒŒ : ใ…‡ใ…ใ…‡? ๋‚˜๋„ ์—†์–ด ๊ทผ๋ฐ ํ•˜๋‚˜ํ•„์š”ํ•˜๋‹ค๋‹ˆ๊นŒ ํ•˜๋‚˜๋งŒ๋“ค์–ด๋“œ๋ฆผ

๊ทธ๋‹ˆ๊นŒ weird ํ•จ์ˆ˜์˜ variable env ์— ์ €์žฅ๋˜๋Š”๊ฒŒ ์•„๋‹˜..
We call this leakage of global variables.

์ € ์ฝ”๋“œ๋ฅผ ์‹ค์ œ๋กœ ์‹คํ–‰ํ•ด๋ณด๋ฉด
window.height ๋ผ๊ณ  ์ „์—ญ๊ฐ์ฒด์— ์ƒ์„ฑ๋˜์–ด์ž‡๋“ฌ...!

์š”๊ฑฐ ๋ฐฉ์ง€ํ•˜๋ ค๋ฉด ‘use strict’์„ ์“ฐ๋ฉด ๋จ
๊ณ ๋Ÿฌ๋ฉด height is not defined ๋ผ๊ณ  ๋ ˆํผ๋Ÿฐ์Šค์—๋Ÿฌ๊ฐ€ ๋‚จ

 

  • ํ•จ์ˆ˜ํ‘œํ˜„์‹์—์„œ ์ต๋ช…ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ๊ธฐ๋ช…ํ•จ์ˆ˜๋ฅผ ์“ฐ๋ฉด

    var hey = function doodle(){
        // doodle();
        return "HEY";
    }
    
    hey();
    doodle();

    doodle์•ˆ์—์„œ doodle์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ž๊ธฐ์ž์‹ (ํ•จ์ˆ˜)์—๋Š” ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ
    ์™ธ๋ถ€์—์„œ๋Š” doodle๋กœ ์ ‘๊ทผ ๋ชปํ•จ

    ๊ธ๊ป˜ doodle ์ด๋ผ๋Š” ์ด๋ฆ„์€ ์ž๊ธฐ์ž์‹ ์˜ ์Šค์ฝ”ํ”„์— enclosed ๋˜์–ด์žˆ์Œ

    hey ๋Š” ๊ธ€๋กœ๋ฒŒ ์ปจํ…์ŠคํŠธ์— ์žˆ๊ธฐ๋•œ์‹œ
    ๊ธ€๋กœ๋ฒŒ์—์„œ๋„ hey๋กœ ๋ถ€๋ฅผ ์ˆ˜ ์žˆ๊ณ 
    ๋‹น๊ทผ doodle ์•ˆ์—์„œ๋„ hey๋กœ ๋ถ€๋ฅผ ์ˆ˜ ์žˆ์Œ

 

43. Global Variables

    <script>var x = 1;</script>
    <script>var y = 2;</script>
    <script>var z = 3;</script>
    <script>var x = 5000;</script>

์ „์—ญ๋ณ€์ˆ˜๋ฅผ ์ง€์–‘ํ•ด์•ผํ•จ
๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ๋„ ์ •์˜ํ•˜๋ฉด ์•ž์„  ์ „์—ญ๋ณ€์ˆ˜x๋ฅผ overwriteํ•˜๊ฒŒ๋จ

Avoid polluting the global namespace or scope when possible.

 

44. IIFE

IIFE - Immediately Invoked Function Expression ์ฆ‰์‹œ์‹คํ–‰ํ•จ์ˆ˜

๋กœ์ปฌ์Šค์ฝ”ํ”„์•ˆ์— ๊ฐ€๋‘๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ

var script1 = (function () {
  function foo() { return "Hi"; }
  return { foo };
})();

script1.foo();    // Hi

์ด๋Ÿฐ์‹์œผ๋กœ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

 

์ถœ์ฒ˜ : Udemy - JavaScript: The Advanced Concept
https://www.udemy.com/course/advanced-javascript-concepts

๋Œ“๊ธ€