본문 바로가기
javascript/javascript 심화지식

함수 유효범위 : 전역변수와 지역변수

by honey.kikiki 2021. 7. 16.
728x90

함수 유효범위 : 전역변수와 지역변수

유효범위(Scope)는 변수의 수명을 의미한다. 아래의 예제를 보자. 결과는 global이다.

var vscope = "global"; // 전역변수 global

function fscope() {
  var vscope = "local"; // 지역변수 local
  // alert(vscope); // 'local' 함수내부위 vscope가 없을떄 "global"
}

let a = "sc";

console.log(a); // sc

function fscope2() {
  // alert(vscope); // "global"
  var lv = "local variables";
  a = "kk";
  console.log(a); //kk
}

fscope();
fscope2();

console.log(lv); // error 지역변수는 밖에서 접근할수가 없다
console.log(a); // kk

function a() {
  let i = 0;
}

for (let i = 0; i < 5; i++) {
  a();
  document.write(i); //01234
}

document.write(i); // error

 

 

전역변수

let MYAPP = {}; // 전역변수vcv

MYAPP.calculator = {
  left: null,
  right: null,
};

MYAPP.coordinate = {
  left: null,
  right: null,
};

MYAPP.calculator.left = 10;
MYAPP.calculator.right = 20;

function sum() {
  return MYAPP.calculator.left + MYAPP.calculator.right;
}

document.write(sum()); // 30

 

// 전역변수를 사용하지않을떄

(function () {
  let MYAPP = {}; //지역변수

  MYAPP.calculator = {
    left: null,
    right: null,
  };

  MYAPP.coordinate = {
    left: null,
    right: null,
  };

  MYAPP.calculator.left = 10;
  MYAPP.calculator.right = 20;

  function sum() {
    return MYAPP.calculator.left + MYAPP.calculator.right;
  }

  document.write(sum());
})(); // 익명함수 방법

 

유효범위의 대상 (함수)

for (let i = 0; i < 1; i++) {
let name = "coding everybody";
let key = 123;

}

console.log(name);
console.log(key);

 

정적유호면위(lexical scoping)

let i = 5; // 전역변수

function a() {
  let i = 10; // 지역변수
  b();
}

function b() {
  document.write(i); // 5
}

a();

'javascript > javascript 심화지식' 카테고리의 다른 글

클로저(Closure)  (0) 2021.07.17
값으로서의 함수와 콜백  (0) 2021.07.17
상속, prototype  (0) 2021.07.13
call, apply, bind  (0) 2021.07.13
setTimeout /setInterval  (0) 2021.07.12

댓글