自调用函数可以在 IE 严格模式下工作吗?

sir*_*lot 4 javascript firefox internet-explorer

我测试了以下代码:

$(function () {
    "use strict"
    (function () {
        console.log("something");
    }());
});
Run Code Online (Sandbox Code Playgroud)

但在 IE 中运行时,我不断收到异常:“预期函数”。在 Firefox 中这工作得很好。这看起来像是基本的功能。我究竟做错了什么?

Poi*_*nty 5

自动分号插入的规则非常奇怪。是否以利用该功能的方式进行编码是一个激烈争论的问题,因此我不会讨论这一点,但在这种情况下,解析器认为您可能正在尝试调用函数。在字符串后面添加分号应该可以解决这个问题。

您可以尝试的另一件事:

$(function () {
    "use strict"
    !function () {
        console.log("something");
    }();
});
Run Code Online (Sandbox Code Playgroud)

(我个人只是添加分号:-)