Which Ecmascript 6 Features Imply Strict Mode

Which ECMAScript 6 features imply strict mode?

The spec says:

  • Module code is always strict mode code.
  • All parts of a ClassDeclaration or a ClassExpression are strict mode code.

The rest are just the known things from ES5, basically every global/eval/function code that begins with the "use strict"; directive. It does work within the new ES6 function kinds (arrow, generator, method syntax) as well.

Not recommended to use use strict in ES6?

ES6 modules are always in strict mode. To quote the relevant part of the spec:

10.2.1 Strict Mode Code


An ECMAScript Script syntactic unit may be processed using either unrestricted or strict mode syntax and semantics. Code is interpreted as strict mode code in the following situations:

  • Global code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive (see 14.1.1).
  • Module code is always strict mode code.
  • All parts of a ClassDeclaration or a ClassExpression are strict mode code.
  • Eval code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive or if the call to eval is a direct eval (see 12.3.4.1) that is contained in strict mode code.
  • Function code is strict mode code if the associated FunctionDeclaration, FunctionExpression, GeneratorDeclaration, GeneratorExpression, MethodDefinition, or ArrowFunction is contained in strict mode code or if the code that produces the value of the function’s [[ECMAScriptCode]] internal slot begins with a Directive Prologue that contains a Use Strict Directive.
  • Function code that is supplied as the arguments to the built-in Function and Generator constructors is strict mode code if the last argument is a String that when processed is a FunctionBody that begins with a Directive Prologue that contains a Use Strict Directive.

What does use strict do in JavaScript, and what is the reasoning behind it?

Update for ES6 modules

Inside native ECMAScript modules (with import and export statements) and ES6 classes, strict mode is always enabled and cannot be disabled.

Original answer

This article about Javascript Strict Mode might interest you: John Resig - ECMAScript 5 Strict Mode, JSON, and More

To quote some interesting parts:

Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a "strict" operating context. This strict context prevents certain actions from being taken and throws more exceptions.

And:

Strict mode helps out in a couple ways:

  • It catches some common coding bloopers, throwing exceptions.
  • It prevents, or throws errors, when relatively "unsafe" actions are taken (such as gaining access to the global object).
  • It disables features that are confusing or poorly thought out.

Also note you can apply "strict mode" to the whole file... Or you can use it only for a specific function (still quoting from John Resig's article):

// Non-strict code...

(function(){
"use strict";

// Define your library strictly...
})();

// Non-strict code...

Which might be helpful if you have to mix old and new code ;-)

So, I suppose it's a bit like the "use strict" you can use in Perl (hence the name?): it helps you make fewer errors, by detecting more things that could lead to breakages.

Strict mode is now supported by all major browsers.

Are there js environments which support strict mode only?

Is there an environment which supports only strict mode?

You can configure Node.js to do that, see Any way to force strict mode in node?:

node --use_strict

JavaScript: Can ECMAScript 5's Strict Mode (use strict) be enabled using single quotes ('use strict')?

For you, without using a browser that supports strict mode:

A Use Strict Directive is an ExpressionStatement in a Directive Prologue whose StringLiteral is either the exact character sequences "use strict" or 'use strict'. A Use Strict Directive may not contain an EscapeSequence or LineContinuation.

What is strict mode and how is it used?

Its main purpose is to do more checking.

Just add "use strict"; at the top of your code, before anything else.

For example, blah = 33; is valid JavaScript. It means you create a completely global variable blah.

But in strict mode it's an error because you did not use the keyword "var" to declare the variable.

Most of the time you don't mean to create global variables in the middle of some arbitrary scope, so most of the time that blah = 33 is written it is an error and the programmer didn't actually want it to be a global variable, they meant to write var blah = 33.

It similarly disallows a lot of things that are technically valid to do. NaN = "lol" does not produce an error. It also doesn't change the value of NaN. Using strict this (and similar weird statements) produce errors. Most people appreciate this because there is no reason to ever write NaN = "lol", so there was most likely a typo.

Read more at the MDN page on strict mode.

Should I 'use strict' for every single javascript function I write?

On this question, do beware a general tendency to oversimplify.

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. (More on this here, from Crockford.)

However, that doesn't address how you should ensure that your code runs in strict mode. There are at least two contexts to consider:

In the browser, your code should be delivered after being minified. If you include 'use strict' in every function body, your minifier won't strip it out, and you'll waste bytes repeating it everywhere. You only need it in your outermost function scope(s)—at the top of your module definitions. One approach is to wrap your minified code in a single IIFE closure as part of the build process:

;(function (){ 
'use strict'
// All code here.
}())

This, I think, is close to the ideal way of doing it, but it more or less dictates that you adopt a continuous integration workflow (since, to observe your code running in strict mode, you must have it all wrapped up one closure). If you don't work that way, you'll have to include the use strict directive at the top of every function that isn't being declared within another function's scope.

On the server, it's much simpler, of course. Code isn't minified, and bytes don't matter, so you can just include the use strict directive at the top of every file.

A word of warning on --use_strict: In cases where you control how scripts are run, you may find yourself tempted to use the --use_strict runtime flag. It's easy, but it means all dependencies must be strict mode compliant. Since you can't control every third-party's compliance, this is usually unwise.



Related Topics



Leave a reply



Submit