In a book trying to illustrate global/local variable scope it used this example to convey the concept:
test();
if (isset(bar)){
alert('bar must be global!');
}else{
alert('bar must be local!');
}
function test(){
var foo = 3;
bar = 5;
}
function isset(varname){
return typeof varname != 'undefined';
}
Right now the if returns the alert 'bar must be global.', which makes sense because the variable passed to the isset()
, bar, in this case is global. But why doesn't the if statement return the alert('foo must be local'); when you pass lets say when you pass foo
in the isset function.
Since you haven't used the var keyword, the variable is defined on global scope and still exists after executing test function. This is explained more thoroughly here.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다