匿名函数 function anonymous( ) 和直接写 function() 有区别吗?
資深大佬 : lisisi 6
报错的时候有区别,前者能追踪到函数名称
大佬有話說 (11)
——————
var a = function() {
console.log(a)
}
var b = a
a = 1
b() // 1
不过一般没人这么写,也就面试可能会考.
为匿名函数命名,可以在这个函数内部递归调用自身提供一个访问的方式
与这个函数名问题有点关系
> (function(){haha})()
Uncaught ReferenceError: haha is not defined
at repl:1:13
> (function a(){haha})()
Uncaught ReferenceError: haha is not defined
at a (repl:1:15)
> (a=function(){haha})()
Uncaught ReferenceError: haha is not defined
at a (repl:1:15)
注意看错误提示的内容是不一样的,后面两个会告诉你是“函数 a”出了错。