[자바스크립트] Scope chain & hoisting
Scope 이란?
The space in which variables and statements can be accessed. It makes it possible to have variables with the same name without colliding with one another and prevents outer scopes from having access to inner scopes.
3 Types of scope
1) global scope
- The default scope - only one in the program
- The top scope that englobes the entire code
- The declarations inside this space can be accessed anywhere in the code
2) function/local scope
- Variables declared inside a function.
- They can only be accessed from whthin that function.
- var is function scope so can be accessed within the function.
3) block scope
- let, const (ES6+)
- They can be scoped to the nearest pair of curly braces (can't be accessed from outside).
Scope chain
Lexical Scope (Static Scope) 이란?
It is determined at the lexing time(generally reffered to as compiling) rather than at runtime.
Dynamic scope: 함수를 어디서 호출하였는지
Lexical scope: 함수를 어디서 선언하였는지
Hoisting (호이스팅, 끌어올림) 이란?
: Makes some types of variables accessible / usable in the code before they are actually declared. Before execution, code is scanned for variable declarations, and for each variable a new property is created in the variable environment object.
함수 안에 있는 선언들을 모두 끌어올려서 해당 함수 유효 범위의 최상단에 선언하는 것. 즉, 함수 내에서 아래쪽에 존재하는 내용 중 필요한 값들을 끌어올리는 것. (let, const는 해당 안됨.)
* 시간날 때 더 자세히 공부하기
https://www.w3schools.com/js/js_scope.asp
JavaScript Scope
JavaScript Scope Scope determines the accessibility (visibility) of variables. JavaScript has 3 types of scope: Block scope Function scope Global scope Block Scope Before ES6 (2015), JavaScript had only Global Scope and Function Scope. ES6 introduced two i
www.w3schools.com
https://developer.mozilla.org/en-US/docs/Glossary/Hoisting
Hoisting - MDN Web Docs Glossary: Definitions of Web-related terms | MDN
JavaScript Hoisting refers to the process whereby the interpreter allocates memory for variable and function declarations prior to execution of the code. Declarations that are made using var are initialized with a default value of undefined. Declarations m
developer.mozilla.org