JavaScript中函数function fun,{}和 var fun=function

标题有点长····

废话少说,其实他们的主要区别就是“函数声明的提前行为”.

var fun=function(){
    alert("hello world!");
};
fun();       //hello world!

/**********************************/

function fun() {
    alert("hello world!");
}
fun();       //hello world!

正常情况下两种方式都会进行正常的编译,并输出“hello world!”,下面把函数调用放在上面再测一下。

fun();       //报错
var fun=function(){
alert("hello world!");
};
/**********************************/
fun();       //hello world!
function fun() {
alert("hello world!");
}

前者不会被提升,后者被提升到“顶部”