简体   繁体   中英

Do self-invoking functions work in IE strict-mode?

I tested the following code:

$(function () {
    "use strict"
    (function () {
        console.log("something");
    }());
});

but when run in IE, I keep getting an exception: "Function Expected". In Firefox this works fine. This seems like basic, functionality. What am I doing wrong?

The rules of automatic semicolon insertion are pretty bizarre. It's a hotly-contested point whether to code in a way that takes advantage of that feature, so I won't get into that, but in this case what's happening is that the parser thinks that you may be trying to call a function. Adding a semicolon after the string should fix that.

Another thing you could try:

$(function () {
    "use strict"
    !function () {
        console.log("something");
    }();
});

(Personally I'd just add the semicolon :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM