[기술면접/JS] dynamic scope(동적 스코프), lexical scope(렉시컬 스코프)

강민혁·2023년 3월 6일
0

기술면접 | JS

목록 보기
12/17

JS에서 dynamic scope(동적 스코프), lexical scope(렉시컬 스코프)에 대해 설명하세요

Keyword

상위 scope, 함수가 정의된 위치, 함수가 실행되는 위치


Script

먼저 자바스크립트의 함수에서 상위 scope를 결정하는 방식은 lexical scope입니다. lexical scope는 함수가 정의된 위치에 따라서 함수의 상위 scope를 결정합니다. 그래서 이 방식을 static scope(정적 스코프)라고도 부릅니다. 반면, dynamic scope는 함수가 실행되는 위치에 따라서 함수의 상위 스코프를 결정합니다.


Additional

code(js)

let x = 1;

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

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

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

Reference

BOOK - modern javascript deep dive

profile
with programming

0개의 댓글