SCOPE

μŠ€μ½”ν”„λŠ” μ°Έμ‘° λŒ€μƒ μ‹λ³„μž(identifier, λ³€μˆ˜, ν•¨μˆ˜μ˜ 이름과 같이 μ–΄λ–€ λŒ€μƒμ„ λ‹€λ₯Έ λŒ€μƒκ³Ό κ΅¬λΆ„ν•˜μ—¬ 식별할 수 μžˆλŠ” μœ μΌν•œ 이름)λ₯Ό μ°Ύμ•„λ‚΄κΈ° μœ„ν•œ κ·œμΉ™μ΄λ‹€. μ‹λ³„μžλŠ” μžμ‹ μ΄ μ–΄λ””μ—μ„œ μ„ μ–ΈλλŠ”μ§€μ— μ˜ν•΄ μžμ‹ μ΄ μœ νš¨ν•œ λ²”μœ„λ₯Ό κ°–λŠ”λ‹€.

var x = 'global';

function foo(){
  var x = 'function scope';
  console.log(x);
}
foo(); // 'function scope'
console.log(x); // global

이름이 같은 λ³€μˆ˜ xκ°€ 쀑볡 μ„ μ–Έλ˜μ–΄μžˆλŠ” μ˜ˆμ‹œμ΄λ‹€. μ „μ—­μœΌλ‘œ μ„ μ–Έλœ xλŠ” μ–΄λ””μ—μ„œλ“  μ°Έμ‘°ν•  수 μžˆμ§€λ§Œ foo ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ μ„ μ–Έλœ xλŠ” λ‚΄λΆ€μ—μ„œλ§Œ μ°Έμ‘°ν•  수 있고, ν•¨μˆ˜ μ™ΈλΆ€μ—μ„œλŠ” μ°Έμ‘°ν•  수 μ—†λ‹€. μ΄λŸ¬ν•œ κ·œμΉ™μ„ μŠ€μ½”ν”„λΌκ³  ν•œλ‹€. λͺ¨λ“  λ³€μˆ˜λŠ” μŠ€μ½”ν”„λ₯Ό 가진닀.

  • μ „μ—­ μŠ€μ½”ν”„ ( Global Scope ) : μ½”λ“œ μ–΄λ””μ—μ„œλ“ μ§€ μ°Έμ‘° κ°€λŠ₯

  • 지역 μŠ€μ½”ν”„ ( Local Scope or Function-Level Scope) : ν•¨μˆ˜ μ½”λ“œ 블둝이 λ§Œλ“  μŠ€μ½”ν”„λ‘œ ν•¨μˆ˜ μžμ‹ κ³Ό ν•˜μœ„ ν•¨μˆ˜μ—μ„œλ§Œ μ°Έμ‘° κ°€λŠ₯

타 μ–Έμ–΄λŠ” 블둝 레벨 μŠ€μ½”ν”„ ( block-level scope )λ₯Ό λ”°λ₯Έλ‹€. ν•˜μ§€λ§Œ μžλ°”μŠ€ν¬λ¦½νŠΈλŠ” ν•¨μˆ˜ 레벨 μŠ€μ½”ν”„( function-level scope ) λ₯Ό λ”°λ₯Έλ‹€. ν•¨μˆ˜ 레벨 μŠ€μ½”ν”„λž€ ν•¨μˆ˜ μ½”λ“œ 블둝 λ‚΄μ—μ„œ μ„ μ–Έλœ λ³€μˆ˜λŠ” ν•¨μˆ˜ μ½”λ“œ λΈ”λ‘λ‚΄μ—μ„œλ§Œ μœ νš¨ν•˜λ©°, ν•¨μˆ˜ μ™ΈλΆ€μ—μ„œλŠ” μœ νš¨ν•˜μ§€ μ•Šλ‹€.

단, ECMAScript 6μ—μ„œ λ„μž…λœ let ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ 블둝 레벨 μŠ€μ½”ν”„λ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€.

var x = 0;
{
  var x = 1;
  console.log(x); // 1
}
console.log(x);   // 1

let y = 0;
{
  let y = 1;
  console.log(y); // 1
}
console.log(y);   // 0

μ „μ—­ λ³€μˆ˜λŠ” μ „μ—­ 객체(Global Object)인 window 의 ν”„λ‘œνΌν‹°μ΄λ‹€.

var x = 'global';

function foo(){
  var x = 'function scope';
  console.log(x);
}
console.log(window.x);  // 'global';

μ „μ—­ λ³€μˆ˜μ˜ μ‚¬μš©μ€ λ³€μˆ˜ 이름이 쀑볡될 수 있으며, μ˜λ„μΉ˜ μ•Šμ€ μž¬ν• λ‹Ήμ— μ˜ν•œ μƒνƒœ λ³€ν™”λ‘œ μ½”λ“œλ₯Ό μ˜ˆμΈ‘ν•˜κΈ° μ–΄λ ΅κ²Œ λ§Œλ“œλ―€λ‘œ μ‚¬μš©μ„ μ–΅μ œν•΄μ•Όν•œλ‹€.

Lexical Scope

lexical scopeλŠ” ν•¨μˆ˜λ₯Ό μ–΄λ””μ„œ ν˜ΈμΆœν•˜λŠ”μ§€κ°€ μ•„λ‹ˆλΌ 어디에 μ„ μ–Έν•˜μ˜€λŠ”μ§€μ— 따라 κ²°μ •λœλ‹€. μžλ°”μŠ€ν¬λ¦½νŠΈλŠ” lexical scopeλ₯Ό λ”°λ₯΄λ―€λ‘œ ν•¨μˆ˜λ₯Ό μ„ μ–Έν•œ μ‹œμ μ— μƒμœ„ μŠ€μ½”ν”„κ°€ κ²°μ •λœλ‹€.

var x = 1;

function foo() {
  var x = 10;
  bar();
}

function bar() {
  console.log(x);
}

foo(); // 1
bar(); // 1

lexical scopeλ₯Ό λ”°λ₯΄λ―€λ‘œ, foo(), bar() ν˜ΈμΆœμ‹œ 1이 좜λ ₯λ˜λŠ” 것을 확인할 수 μžˆλ‹€.

μ΅œμ†Œν•œμ˜ μ „μ—­λ³€μˆ˜ μ‚¬μš©

μžλ°”μŠ€ν¬λ¦½νŠΈλŠ” λ³€μˆ˜λͺ…μ˜ 쀑볡을 ν—ˆμš©ν•˜λ―€λ‘œ, μ „μ—­λ³€μˆ˜μ˜ λ¬΄λΆ„λ³„ν•œ μ‚¬μš©μ€ 무척 μœ„ν—˜ν•˜λ‹€. μ „μ—­ λ³€μˆ˜λ₯Ό λ°˜λ“œμ‹œ μ‚¬μš©ν•΄μ•Όν•  μ΄μœ κ°€ μ—†λ‹€λ©΄, μ§€μ—­λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜κ³ , λ³€μˆ˜μ˜ λ²”μœ„μΈ μŠ€μ½”ν”„λŠ” μ’μ„μˆ˜λ‘ μ’‹λ‹€.

var MYAPP = {};
MYAPP.student = {
  name: 'Faker',
  gender: 'male'
};

μ „μ—­λ³€μˆ˜λ₯Ό μ΅œμ†Œν™”ν•˜λŠ” 방법 쀑 ν•˜λ‚˜λŠ” μ „μ—­λ³€μˆ˜ 객체λ₯Ό ν•˜λ‚˜ μƒμ„±ν•΄μ„œ μ‚¬μš©ν•˜λŠ” 것이닀.

μ¦‰μ‹œ μ‹€ν–‰ ν•¨μˆ˜

μ¦‰μ‹œ μ‹€ν–‰ ν•¨μˆ˜(IIFE, Immediately-Invoked Function Expression)λ₯Ό μ‚¬μš©ν•˜λ©΄, μ „μ—­λ³€μˆ˜ μ‚¬μš©μ„ μ–΅μ œν•  수 μžˆλ‹€.

(function () {
  var MYAPP = {};

   MYAPP.student = {
    name: 'Faker',
    gender: 'male'
  };

  console.log(MYAPP.student.name); // Faker
}());

console.log(MYAPP.student.name);
VM1857:12 Uncaught ReferenceError: MYAPP is not defined
    at <anonymous>:12:13

이 방법을 μ‚¬μš©ν•˜λ©΄ μ „μ—­λ³€μˆ˜λ₯Ό λ§Œλ“€μ§€ μ•ŠμœΌλ―€λ‘œ 라이브러리 λ“±μ—μ„œ 자주 μ‚¬μš©λ˜λ©°, μ¦‰μ‹œ μ‹€ν–‰ ν•¨μˆ˜λŠ” μ¦‰μ‹œ μ‹€ν–‰λ˜κ³  κ·Έ ν›„ λ°”λ‘œ 사라진닀.

μ°Έμ‘° νŽ˜μ΄μ§€

Last updated

Was this helpful?