JavaScript中函数function fun(){}和 var fun=function(){}的区别

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!");
} 

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

posted @ 2017-08-11 17:53  七尚  阅读(899)  评论(0编辑  收藏  举报