Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 458
Buksy Avatar asked Nov 09 '15 07:11

Buksy


People also ask

What is strict mode?

StrictMode is a tool for highlighting potential problems in an application. Like Fragment , StrictMode does not render any visible UI. It activates additional checks and warnings for its descendants. Note: Strict mode checks are run in development mode only; they do not impact the production build.

What means use strict?

The "use strict" Directive The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode, you can not, for example, use undeclared variables. All modern browsers support "use strict" except Internet Explorer 9 and lower: Directive.

Should I use strict mode JavaScript?

First, all of your code absolutely should be run in strict mode. Core modern javascript functionality is changed (see . call() and apply()) or disfigured (silent Errors) by executing code outside of strict mode.


1 Answers

"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";.

like image 192
Amadan Avatar answered Sep 28 '22 01:09

Amadan