Strict mode and reserved word

Why is this code ok:

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

        var interface               = new Object(this);
    }
}

Until it

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 that the interface is reserved in strict mode, but shouldn't both examples throw an error?

+4
source share
2 answers

"use strict";must be the first statement in a function (or in a script, if script is -wide) to call strict mode; anywhere else you can write "merry christmas";.

+7
source

The first example does not actually enforce strict mode. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#Invoking_strict_mode :

. , {} ; .

+4

Source: https://habr.com/ru/post/1615006/


All Articles