Search code examples
javascriptstrict-mode

Strict mode and reserved word


Why is this code fine:

var test = {
    fn1: function(_origin, _componentType) {
        if(arguments.length > 1) throw "xx";
        // this strict is ok
        "use strict";

        var interface               = new Object(this);
    }
}

While this isn't

var test = {
    fn1: function(_origin, _componentType) {
        // This strict throws SyntaxError
        "use strict";

        if(arguments.length > 1) throw "xx";
        var interface               = new Object(this);
    }
}

I know interface is reserved word in strict mode, but shouldn't both examples throw an error?


Solution

  • "use strict"; needs to be the first statement in a function (or in a script, if script-wide) to trigger strict mode; anywhere else, you may as well be writing "merry christmas";.