对于变量提升:
当变量前面没有var时,不存在提升
当变量前面有var时,才存在提升
<script type="text/javascript">
function test8(){
alert(a);
var a="hello world";
}
test8();//undefined
alert("执行了......");//这句话执行了
</script>
<script type="text/javascript">
function test9(){
alert(a);
a="hello world";
}
test9();//出错
alert("未执行......");//这句话未执行
</script>
对于函数提升:
<1> 只有函数声明才会进行函数提升,函数表达式不存在提升.
<2> 函数提升会将函数体一起提升上去,这点与变量提升有所不同
//函数声明
foo() // 调用成功
function foo() {}
//函数表达式
foo() // 有foo这个变量,但它却不是函数,所以调用失败
var foo = function() {}
函数的提升大于变量的提升