Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 534
sircodesalot Avatar asked Oct 19 '25 21:10

sircodesalot


1 Answers

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

like image 199
Pointy Avatar answered Oct 22 '25 10:10

Pointy