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!");
}
前者不会被提升,后者被提升到“顶部”
最近在维护一个前端交流群,群内有各个领域的大佬,各路妹子,程序员鼓励师等你来撩,欢迎加入,群号:249620372