var与function区别

2019-03-02  本文已影响0人  黑色的五叶草

var a = function(){}function a(){} 有什么区别

function b(){
    document.write("aa");
}

var a=function(){
    document.write("123");
}

b();
a();

好像没什么区别

b();
a();

function b(){
    document.write("aa");
}

var a=function(){
    document.write("123");    // Uncaught TypeError: a is not a function
}

这样再运行一下就有区别了

function b(){} 为函数声明,程序运行前就已存在;
var a = function(){} 为函数表达式,属于按顺序执行,所以aundefined

当调用a(),因为a不是function所以抛出Uncaught TypeError: a is not a function错误

上一篇 下一篇

猜你喜欢

热点阅读