사례 1 : 다음 코드는 경고 예상대로 :전역 변수는 Javascript의 함수 범위보다 우선 적용됩니까?
var globalId='10';
function check(){
alert(globalId);
}
check();
사례 2 : 그러나 다음 코드 경고 정의되지 않은 : 를 들어
var globalId='10';
function check(){
alert(globalId);
var globalId;
}
check();
사례 2솔루션은 다음과 같습니다
var globalId='10';
function check(){
var globalId; /**moved to top of the function due to hoisting and has an initial value of undefined. Hence, alert(globalId) return undefined. **/
alert(globalId); //undefined
var globalId;
}
check();
내 질문은 지금, 어떻게 1 globalId
이 10
의 값을 갖는 경우에합니까?
로컬이 없으면 전역에 도달 할 때까지 외부를 찾기 시작합니다. – dandavis
첫 번째 경우 –
에서 범위 수준 변수를 선언하지 않으므로 함수 내에서 접두사'var'를 사용하여 case 2에서 var private를 만들기 때문에 ... – pirs