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

前端 未结 28 3131
半阙折子戏
半阙折子戏 2020-11-21 06:05

Recently, I ran some of my JavaScript code through Crockford\'s JSLint, and it gave the following error:

Problem at line 1 character 1: Missing \"use

相关标签:
28条回答
  • 2020-11-21 06:37

    Strict mode makes several changes to normal JavaScript semantics:

    • eliminates some JavaScript silent errors by changing them to throw errors.

    • fixes mistakes that make it difficult for JavaScript engines to perform optimizations.

    • prohibits some syntax likely to be defined in future versions of ECMAScript.

    for more information vistit Strict Mode- Javascript

    0 讨论(0)
  • 2020-11-21 06:38

    I strongly recommend every developer to start using strict mode now. There are enough browsers supporting it that strict mode will legitimately help save us from errors we didn’t even know were in your code.

    Apparently, at the initial stage there will be errors we have never encountered before. To get the full benefit, we need to do proper testing after switching to strict mode to make sure we have caught everything. Definitely we don’t just throw use strict in our code and assume there are no errors. So the churn is that it’s time to start using this incredibly useful language feature to write better code.

    For example,

    var person = {
        name : 'xyz',
        position : 'abc',
        fullname : function () {  "use strict"; return this.name; }
    };
    

    JSLint is a debugger written by Douglas Crockford. Simply paste in your script, and it’ll quickly scan for any noticeable issues and errors in your code.

    0 讨论(0)
  • 2020-11-21 06:39

    Quoting from w3schools:

    The "use strict" Directive

    The "use strict" directive is new in JavaScript 1.8.5 (ECMAScript version 5).

    It is not a statement, but a literal expression, ignored by earlier versions of JavaScript.

    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.

    Why Strict Mode?

    Strict mode makes it easier to write "secure" JavaScript.

    Strict mode changes previously accepted "bad syntax" into real errors.

    As an example, in normal JavaScript, mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a global variable.

    In normal JavaScript, a developer will not receive any error feedback assigning values to non-writable properties.

    In strict mode, any assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object, will throw an error.

    Please refer to http://www.w3schools.com/js/js_strict.asp to know more

    0 讨论(0)
  • 2020-11-21 06:39

    There's a good talk by some people who were on the ECMAScript committee: Changes to JavaScript, Part 1: ECMAScript 5" about how incremental use of the "use strict" switch allows JavaScript implementers to clean up a lot of the dangerous features of JavaScript without suddenly breaking every website in the world.

    Of course it also talks about just what a lot of those misfeatures are (were) and how ECMAScript 5 fixes them.

    0 讨论(0)
  • 2020-11-21 06:40

    Note that use strict was introduced in EcmaScript 5 and was kept since then.

    Below are the conditions to trigger strict mode in ES6 and ES7:

    • 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.
    0 讨论(0)
  • 2020-11-21 06:40

    JavaScript “strict” mode was introduced in ECMAScript 5.

    (function() {
      "use strict";
      your code...
    })();
    

    Writing "use strict"; at the very top of your JS file turns on strict syntax checking. It does the following tasks for us:

    1. shows an error if you try to assign to an undeclared variable

    2. stops you from overwriting key JS system libraries

    3. forbids some unsafe or error-prone language features

    use strict also works inside of individual functions. It is always a better practice to include use strict in your code.

    Browser compatibility issue: The "use" directives are meant to be backwards-compatible. Browsers that do not support them will just see a string literal that isn't referenced further. So, they will pass over it and move on.

    0 讨论(0)
提交回复
热议问题