这段代码有什么问题吗?
function test() {
(function(){
console.log('1')
})()
(function(){
console.log('2')
})()
}
test()
最佳答案
你错过了每个函数调用末尾的分号......
function test() {
(function(){
console.log('1');
})();
(function(){
console.log('2');
})();
}
test();
Here is a JSFiddle如果您需要测试工作代码。例如,在 Chrome 中,您可以右键单击 > 检查元素 > 并切换到“控制台”选项卡
感谢@pimvdb 指出当你没有分号时这实际上试图做什么:
It is currently trying to pass the second function as an argument to the result of the first.
关于javascript - 2 以下自调用函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8523908/