1. Scope 란?
유효범위란 뜻으로, Identifier (참조 대상 식별자)를 찾아내기 위한 규칙을 의미한다.
2. Scope
- 전역 범위 : 스크립트 모드에서 실행되는 모든 코드의 기본 범위
- 모듈 범위 : 모듈 모드에서 실행되는 코드의 범위
- 함수 범위 ( Function Level Scope ): function으로 생성된 범
- 블록 범위 ( Block Level Scope ) : 중괄호로 생성된 범위, let & const로 선언된 변수
블록내에서 유효
let y = 0;
{
let y = 1;
console.log(y); // 1
}
console.log(y); // 0
함수 코드 블록 내에서 선언된 변수는 함수 코드 블록 내에서만 유효
function example(){
var x = 1;
console.log(x); // 1
}
console.log(x); // error
2.Hoisting 란?
호이스팅이란 변수의 정의가 그 스코프에 따라 선언과 할당으로 분리되는 것을 의미.
선엄 부분이 해당 스코프의 최상위로 변경된다.
3. Hoisting
function hoisting(){
text = "javascript";
var text;
console.log(text);
}
hoisting();
// 출력
// hoisting
//Hoisting된 코드
function hoisting(){
var text;
text = "javascript";
console.log(text);
}
function hoisting(){
console.log(1, text);
var text = "javascript";
console.log(2, text);
}
hoisting();
// 출력
// 1undefined
// 2hoisting
//Hoisting된 코드
function hoisting(){
var text;
console.log(1, text);
text = "javascript";
console.log(2, text);
}
반응형
'Client Side' 카테고리의 다른 글
Save screen as PDF (0) | 2025.01.02 |
---|---|
SPA & SSG & SSR (0) | 2024.07.16 |
Cannot read properties of undefined / null & undefined (1) | 2023.12.06 |
forEach, for, every, some : 반복문 (0) | 2023.04.11 |
forEach 순차처리의 불가능 (for문, for of문의 순차처리) (0) | 2023.04.06 |