小问题,在自执行函数前定义变量的问题

 
var obj = {
    name:'nan'
}
obj.fun = function(){
    console.log(1)
}
(
function(){ console.log(2) })()

这样会输出

1
VM29719:6 Uncaught TypeError: (intermediate value)(...) is not a function

因为没有在定义obj.fun时添加分号,导致

obj.fun = function(){
    console.log(1)
}(function(){console.log(2)})

定义时直接执行一次,后面的括号导致报错

posted on 2018-06-06 14:07  weblsy  阅读(176)  评论(0)    收藏  举报