var与function区别

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错误

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容