Javascript - Strict Mode

> (World Wide) Web - (W3|WWW) > Javascript (Js|ECMAScript) > ECMAScript - Grammar Standard

1 - About

strict mode makes some operations more strict such as:

  • the definition of this
  • the variable must be created implicitly
  • the data type

strict mode

ES5 introduced the strict mode. This feature allows you to opt in to a restricted version of JavaScript that disallows some of the more problematic or error-prone features of the full language.

Advertising

3 - Example

At the beginning of a scope

"use strict";

Within a function:

function f(x) {
    "use strict";
    .....
}

Evaluating a string literal in Js has no side effects, so an older engine such as ES3 engine executes the directive as an innocuous statement

4 - strict with not strict

Strict and Non Strict mode can live together by wrapping the content in two immediately invoked function expressions (IIFEs).

Example:

// no strict-mode directive at the root
(function() {
    // file1.js
    "use strict";
    function f() {
        // ...
    }
    // ...
})();
(function() {
    // file2.js
    // no strict-mode directive
    function f() {
        var arguments = [];
        // ...
    }
    // ...
})();
web/javascript/es/strict.txt · Last modified: 2019/03/27 10:20 by gerardnico