问题
Does var foo
get hoisted to the top of the stack even when the code inside the false block isn't ever going to be executed?
function foo(){
if ( false ) {
var foo = 'bar'; //will this be hoisted even if its never executed?
}
}
I'm seeing that it is and was just confused...I didn't expect it to get hoisted in its wrapped in a false condition.
回答1:
Yes; the hoisting happens before the code is run, so whether or not the if
statement comes out true
or false
isn't yet known.
来源:https://stackoverflow.com/questions/15648459/does-this-variable-get-hoisted-no-matter-what