๐Ÿš€ Languege/JavaScript

[๊ธฐ๋ณธ ๋ฌธ๋ฒ•] JavaScript ๋ณ€์ˆ˜ (Scope/variable)

mini_world 2021. 4. 14. 23:19
๋ชฉ์ฐจ ์ ‘๊ธฐ

JavaScript๋ฅผ ์ด์ œ ๋ง‰ ์‹œ์ž‘ํ•œ ์ž๋ฐ”๋ฆฐ์ด๋กœ์จ, ๋‚˜์ค‘์— ๋‹ค์‹œ ์ฐพ์•„๋ณผ ์ˆ˜ ์žˆ๋„๋ก ์ •๋ฆฌํ•œ ํฌ์ŠคํŒ…์ž…๋‹ˆ๋‹ค. ๐Ÿ˜‚


1. ๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐฉ๋ฒ•

๋ณ€์ˆ˜ ์„ ์–ธ๋ฌธ ์žฌ์„ ์–ธ ์žฌํ• ๋‹น ํŠน์ง•
let ๋ถˆ๊ฐ€ ๊ฐ€๋Šฅ - ๋ธ”๋ก ๋ ˆ๋ฒจ ์Šค์ฝ”ํ”„
- ECMA2015 ๋ฒ„์ „๋ถ€ํ„ฐ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
const (constant(์ƒ์ˆ˜)) ๋ถˆ๊ฐ€ ๋ถˆ๊ฐ€ - ๋ณ€์ˆ˜ ์„ ์–ธ์‹œ ๊ฐ’์„ ๋ฐ˜๋“œ์‹œ ๊ฐ™์ด ๋„ฃ์–ด์ค˜์•ผ ํ•จ 
- ๋ธ”๋ก ๋ ˆ๋ฒจ ์Šค์ฝ”ํ”„
- ECMA2015 ๋ฒ„์ „๋ถ€ํ„ฐ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
var (variables(๋ณ€์ˆ˜)) ๊ฐ€๋Šฅ ๊ฐ€๋Šฅ - ์ „๋ถ€ ๊ฐ€๋Šฅ (์ž์œ ๋„ ๋†’์Œ)

  1) let

๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ๊ฐ’์„ ๋ฐ”๊ฟ”๋ด…๋‹ˆ๋‹ค.

let tomato

tomato = "๋ง›์žˆ์–ด";
console.log(tomato);

tomato = "๋นจ๊ฐ„์ƒ‰";
console.log(tomato);

/*
์‹คํ–‰ ๊ฒฐ๊ณผ
-----------------------------------
๋ง›์žˆ์–ด
๋นจ๊ฐ„์ƒ‰
-----------------------------------
*/

๋ณ€์ˆ˜๋ฅผ ์žฌ ์„ ์–ธ ํ•ด๋ด…๋‹ˆ๋‹ค.
์ด๋ฏธ ์„ ์–ธ๋˜์—ˆ๋‹ค๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์˜ค๋„ค์š”. let์€ ๊ฐ’์€ ์žฌํ• ๋‹น์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์žฌ์„ ์–ธ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

let tomato

tomato = "๋ง›์žˆ์–ด";
console.log(tomato);

let tomato = "๊ฟ€๋ง›"

/*
์‹คํ–‰ ๊ฒฐ๊ณผ
-----------------------------------
let tomato = "๊ฟ€๋ง›"
    ^

SyntaxError: Identifier 'tomato' has already been declared
-----------------------------------
*/

  2) const

๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด๋ด…๋‹ˆ๋‹ค. const๋Š” ๋ณ€์ˆ˜ ์„ ์–ธ์‹œ ๋ฐ˜๋“œ์‹œ ๊ฐ’์„ ๋„ฃ์–ด์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค. 
์•„๋ž˜ ์ฝ”๋“œ์—์„œ๋Š” ๋ณ€์ˆ˜๋งŒ ์„ ์–ธํ–ˆ๊ธฐ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€๋‚˜์˜ค๋„ค์š”.

const banana

banana = "๋…ธ๋ž‘";
console.log(banana);

/*
์‹คํ–‰ ๊ฒฐ๊ณผ
-----------------------------------
const banana
      ^^^^^^

SyntaxError: Missing initializer in const declaration
-----------------------------------
*/

   ์ด๋ฒˆ์—๋Š” ๊ฐ’์„ ๋„ฃ๊ณ  ์„ ์–ธํ•ด๋ด…๋‹ˆ๋‹ค. ์„ ์–ธ์ด ์ž˜ ๋˜์—ˆ๋„ค์š”.

const banana = "๋…ธ๋ž‘";
console.log(banana);

/*
์‹คํ–‰ ๊ฒฐ๊ณผ
-----------------------------------
๋…ธ๋ž‘
-----------------------------------
*/

์ด์ œ, ์žฌ ํ• ๋‹น์„ ํ•ด๋ด…๋‹ˆ๋‹ค. ๋ฐ”๋‚˜๋‚˜๋Š” ๋ง›์žˆ์–ด๋ฅผ ๋„ฃ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
์—ญ์‹œ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋„ค์š”. const๋Š” ๊ฐ’์„ ์žฌํ• ๋‹นํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 

const banana = "๋…ธ๋ž‘";
console.log(banana);

banana = "๋ง›์žˆ์–ด";
console.log(banana);



/*
์‹คํ–‰ ๊ฒฐ๊ณผ
-----------------------------------
banana = "๋ง›์žˆ์–ด";
       ^

TypeError: Assignment to constant variable.
-----------------------------------
*/

๋ณ€์ˆ˜๋ฅผ ์žฌ์„ ์–ธ ํ•ด๋ด…๋‹ˆ๋‹ค. ๋ฐ”๋กœ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์˜ค๋„ค์š”.
const๋Š” ๋ณ€์ˆ˜๋ฅผ ์žฌ์„ ์–ธํ• ์ˆ˜๋„ ์—†์Šต๋‹ˆ๋‹ค. (์ง€์—ญ๋ณ€์ˆ˜์—์„œ๋Š”..๊ฐ€๋Šฅ)

const banana = "๋…ธ๋ž‘";
console.log(banana);

var banana = "๋ง›์žˆ์–ด";



/*
์‹คํ–‰ ๊ฒฐ๊ณผ
-----------------------------------
var banana = "๋ง›์žˆ์–ด";
    ^

SyntaxError: Identifier 'banana' has already been declared
-----------------------------------
*/

 

  3) var

var ๋Š” ์œ„์—์„œ ๋งํ–ˆ๋˜ ์žฌ์„ ์–ธ, ์žฌํ• ๋‹น ๋ชจ๋‘ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด ์„ ์–ธ๋˜๊ธฐ ์ „์—๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
๋จผ์ €, ์žฌ์„ ์–ธ, ์žฌํ• ๋‹น ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

// ๋ณ€์ˆ˜ ์„ ์–ธ
var apple;

apple = "๋ง›์žˆ์–ด";
console.log(apple);

// ๋ณ€์ˆ˜ ์žฌ ์„ ์–ธ
var apple = "์‚ฌ๊ฐ์‚ฌ๊ฐ";
console.log(apple);

// ๋ณ€์ˆ˜ ์žฌ ํ• ๋‹น
apple = "์•„์˜ค๋ฆฌ์‚ฌ๊ณผ";
console.log(apple);

/*
์‹คํ–‰ ๊ฒฐ๊ณผ
-----------------------------------
๋ง›์žˆ์–ด
์‚ฌ๊ฐ์‚ฌ๊ฐ
์•„์˜ค๋ฆฌ์‚ฌ๊ณผ
-----------------------------------
*/

์„ ์–ธ ์ „์— ์‚ฌ์šฉ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
์œ„์˜ let, const, var ๊ฐ™์ด ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค.

// let ์„ ์–ธ์ „ ์‚ฌ์šฉ
console.log(test_let);
let test_let = "B";

/*
์‹คํ–‰ ๊ฒฐ๊ณผ
-----------------------------------
console.log(test_let);
            ^

ReferenceError: Cannot access 'test_let' before initialization
-----------------------------------
*/


// const ์„ ์–ธ์ „ ์‚ฌ์šฉ 
console.log(test_const);
const test_const = "A";


/*
์‹คํ–‰ ๊ฒฐ๊ณผ
-----------------------------------
console.log(test_const);
            ^

ReferenceError: Cannot access 'test_const' before initialization
-----------------------------------
*/

// var ์„ ์–ธ์ „ ์‚ฌ์šฉ

console.log(test_var);
var test_var = "C";
/*
์‹คํ–‰ ๊ฒฐ๊ณผ
-----------------------------------
undefined
-----------------------------------
*/

 

2. Scpoe(์œ ํšจ๋ฒ”์œ„)

๋”๋ณด๊ธฐ

์™œ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์•ผํ• ๊นŒ? ๋ผ๋Š” ์˜๋ฌธ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๋‹จ์ˆœํžˆ ๋ณด๋ฉด ๋ณ€์ˆ˜์„ ์–ธ ์—†์ด๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ธฐ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

a="a"
var b="b"

console.log(a, b);

/*
์‹คํ–‰ ๊ฒฐ๊ณผ
-----------------------------------
a b
-----------------------------------
*/

์Šค์ฝ”ํ”„๋ž€, ๋ณ€์ˆ˜๊ฐ€ ํ™œ๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ”์œ„๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. 
๋ณ€์ˆ˜์˜ ๋ฒ”์œ„๋ฅผ ํ™•์ธ ํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

const area1 = "A";
let area2 = "B";
var area3 = "C";

function func() {
    const area4 = "D";
    let area5 = "E";
    var area6 = "F";
    const area1 = "a";  // area1 ๋ณ€์ˆ˜ ์žฌ ํ• ๋‹น
    area7 = "G";
    console.log("ํ•จ์ˆ˜๋‚ด๋ถ€์—์„œ ์‹คํ–‰:",area1,area2,area3,area4,area5,area6,area7)
}

console.log("ํ•จ์ˆ˜์™ธ๋ถ€์—์„œ ์‹คํ–‰:",area1,area2,area3)
func();

/*
์‹คํ–‰ ๊ฒฐ๊ณผ
-----------------------------------
ํ•จ์ˆ˜์™ธ๋ถ€์—์„œ ์‹คํ–‰: A B C
ํ•จ์ˆ˜๋‚ด๋ถ€์—์„œ ์‹คํ–‰: a B C D E F G
-----------------------------------
*/

- ์ „์—ญ๋ณ€์ˆ˜(Global) : ์ฝ”๋“œ ๋‚ด ์–ด๋””์„œ๋‚˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
์ „์—ญ๋ณ€์ˆ˜๋Š” ์œ„ ๋ณ€์ˆ˜ ์ค‘ area1, area2, area3์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.
์ „์—ญ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜ ๋ฐ–์— ์„ ์–ธ๋œ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค.

- ์ง€์—ญ๋ณ€์ˆ˜(local) : ํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
์ง€์—ญ๋ณ€์ˆ˜๋Š” ์œ„ ๋ณ€์ˆ˜์ค‘ area4, area5, area6, area7์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.
area1 ๋ณ€์ˆ˜๋Š” ์ „์—ญ๋ณ€์ˆ˜์—์„œ ์ด๋ฏธ ์„ ์–ธํ–ˆ์œผ๋‚˜, ์ง€์—ญ๋ณ€์ˆ˜์—์„œ ํ•œ๋ฒˆ ๋” ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.
๊ทธ ๊ฒฐ๊ณผ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์‹คํ–‰๋ ๋•Œ ์ง€์—ญ๋ณ€์ˆ˜๊ฐ€ ์šฐ์„ ํ•ฉ๋‹ˆ๋‹ค.
์ง€์—ญ๋ณ€์ˆ˜์—์„œ ์„ ์–ธํ•œ ํ•จ์ˆ˜๋Š”, ํ•จ์ˆ˜ ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค (not defined error)

- ์ •๋ฆฌ
1) ๋ณ€์ˆ˜๋Š” ์œ ํšจ๋ฒ”์œ„(Scope)๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
2) ํฌ๊ฒŒ ์ „์—ญ๋ณ€์ˆ˜(Global variable), ์ง€์—ญ๋ณ€์ˆ˜(Local variable)๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค.
3) ๋‚˜๋ˆ„๋Š” ๊ธฐ์ค€์€ ํ•จ์ˆ˜๋ฒ”์œ„, ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋ฅผ ์ง€์—ญ๋ณ€์ˆ˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
4) ํ•จ์ˆ˜ ๋‚ด๋ถ€, ํ˜น์€ ๋ธ”๋ก๋ฌธ(if ๋“ฑ)์—์„œ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ• ๋•Œ, ํ•จ์ˆ˜ ๋‚ด๋ถ€ ํ˜น์€ ๋ธ”๋ก๋ฌธ ๋‚ด๋ถ€์—์„œ ๋ณ€์ˆ˜๋ฅผ ์ฐพ๊ณ , ๋‚ด๋ถ€์— ์—†๋‹ค๋ฉด ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜๋ฅผ ์ฐพ๊ณ , ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ๋™์ ์œผ๋กœ ํ• ๋‹นํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. (area7 ํ™•์ธ)
5) ์ง€์—ญ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜ ๋‚ด์—์„œ ์ „์—ญ๋ณ€์ˆ˜๋ณด๋‹ค ๋” ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

 


์ •๋ง ์•Œ๊ธฐ ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•ด์ฃผ๋Š” ์œ ํŠญ์ž๋ฃŒ๊ฐ€ ์žˆ์–ด์š”~ ์ดํ•ด ์•ˆ๊ฐˆ๋•Œ๋Š” ๋ณต์Šตํ•˜๊ธฐ~~!

www.youtube.com/watch?v=HsJ4oy_jBx0

 

728x90