Home JS[Scope, Closure]
Post
Cancel

JS[Scope, Closure]

Scope

변수의 유효범위

  • let,const
1
2
3
4
5
6
7
8
let username = "moon";
if (username) {
  let message = `hello ${username}`;
  const message2 = `hello ${username}`;
  console.log(message);
}
console.log(message); //ReferenceError: message is not defined
console.log(message2); //ReferenceError: message2 is not defined

let,const는 블록 내에서만 사용가능

  • var
1
2
3
4
5
6
let username = "moon";
if (username) {
  var message = `hello ${username}`;
  console.log(message);
}
console.log(message); //hello moon

var는 블록 스코프를 무시 (모든 블록 스코프를 무시하는건 아님, 화살표 함수의 블록 스코프는 무시하지 않는다.)

블록 단위로 스코프를 구분했을 때, 훨씬 예측 가능한 코드를 작성할 수 있으므로 let,const 키워드 사용이 권장됨

 letconstvar
유효범위블록 스코프 및
함수 스코프
블록 스코프 및
함수 스코프
함수 스코프
값 재할당가능불가능가능
재선언불가능불가능가능

Closure

외부 함수의 변수에 접근할 수 있는 내부 함수

1
2
3
4
5
6
7
8
9
10
11
const adder = (x) => (y) => x + y;
/*const adder = function (x) {
	return function(y){
    	return x+y
    }
}*/
adder(5)(7); // 12

typeof adder(5); // function

adder(5); // y => x + y

클로저는 리턴하는 함수에 의해 스코프가 구분된다. 클로저의 핵심은 스코프를 이용해, 변수의 접근 범위를 닫는데 있다.

클로저는 정보의 접근 제한(캡슐화)에 유용하게 사용된다

This post is licensed under CC BY 4.0 by the author.