반응형
스코프
-
스코프(scope)와 호이스팅(hoisting)Javascript 2023. 4. 10. 23:06
스코프(Scope) 변수의 유효범위. 선언한 변수가 영향을 미치는 범위를 의미함. 호이스팅(Hoisting) 인터프리터가 변수와 함수의 메모리 공간을 선언 전에 미리 할당하는 것 변수의 선언과 초기화를 분리 후, 선언만 코드의 최상단으로 옮기는 것 (자바스크립트는 초기화를 제외한 선언만 호이스팅한다.) → 함수나 변수 선언하기 전에 호출해도 에러가 발생하지 않음 *인터프리터 : 프로그래밍 언어의 소스 코드를 바로 실행하는 프로그램 var 재선언 O, 재할당(타입 변경도 가능) O / 함수 스코프 (또는 전역 스코프) 더보기 블록 기준으로 스코프가 생기지 않아, 코드블록 내에 변수를 선언했어도 해당 변수는 전역변수가 된다.var로 선언한 변수는 어디서든 참조할 수 있으나, 변수에 할당하기 전에는 값은 un..