이것은 가변 호이 스팅과 관련이 있습니다. 이것이 의미하는 바는, 변수 선언 (및 일반적으로 선언)은 코드가 실행되기 전에 처리되며, 코드의 아무 곳에서나 변수를 선언하는 것은 맨 위에 선언하는 것과 동일합니다. 이것은 변수가 선언되기 전에 사용 된 것처럼 보일 수도 있음을 의미합니다.
당신은 다음 작업을 수행 할 때 : 그시에,
첫 번째 경우
var box;
alert(box);
box = "Thinking outside the box"
가 게양되지 않은, 당신은 따라서 어떤 변수 선언을하지 않으며, :
alert(box)
var box = "Thinking outside the box"
이 암시 적으로 이해된다 포인트 박스는 undefined
왜 이런 일이 발생합니까? 측면 읽으면서
For completeness, let’s mention that actually at the implementation level things are a little more complex. There are two stages of the code handling, where variables, function declarations, and formal parameters are created at the first stage, which is the stage of parsing and entering the context. In the second stage, the stage of runtime code execution, function expressions and unqualified identifiers (undeclared variables) are created. But for practical purposes, we can adopt the concept of hoisting, which is actually not defined by ECMAScript standard but is commonly used to describe the behaviour.
- Stoyan Stefanov, "JavaScript Patterns"
, 안전 목자에서 this 기사를 링크 :
의 Stoyan 스테파노는 그의 책 "자바 스크립트 패턴"에서 설명 하듯이는 권상은 자바 스크립트 인터프리터의 구현의 결과이다.
"호이 스팅" – elclanrs
호출 상자에 대해 읽으십시오. 상자를 정의하기 전에 존재하지 않으며 '값'이 있는지 여부에 관계없이 정의 할 때만 존재합니다. – saj
당신의 이해는 꽤 많이 있습니다 :) –