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

 
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)})

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

猜你喜欢

转载自www.cnblogs.com/longsiyuan/p/9144824.html