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

前端 未结 28 3089
半阙折子戏
半阙折子戏 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:47

    Strict mode eliminates errors that would be ignored in non-strict mode, thus making javascript “more secured”.

    Is it considered among best practices?

    Yes, It's considered part of the best practices while working with javascript to include Strict mode. This is done by adding the below line of code in your JS file.

    'use strict';

    in your code.

    What does it mean to user agents?

    Indicating that code should be interpreted in strict mode specifies to user agents like browsers that they should treat code literally as written, and throw an error if the code doesn't make sense.

    For example: Consider in your .js file you have the following code:

    Scenario 1: [NO STRICT MODE]

    var city = "Chicago"
    console.log(city) // Prints the city name, i.e. Chicago
    

    Scenario 2: [NO STRICT MODE]

    city = "Chicago"
    console.log(city) // Prints the city name, i.e. Chicago
    

    So why does the variable name is being printed in both cases?

    Without strict mode turned on, user agents often go through a series of modifications to problematic code in an attempt to get it to make sense. On the surface, this can seem like a fine thing, and indeed, working outside of strict mode makes it possible for people to get their feet wet with JavaScript code without having all the details quite nailed down. However, as a developer, I don't want to leave a bug in my code, because I know it could come back and bite me later on, and I also just want to write good code. And that's where strict mode helps out.

    Scenario 3: [STRICT MODE]

    'use strict';
    
    city = "Chicago"
    console.log(city) // Reference Error: asignment is undeclared variable city.
    

    Additional tip: To maintain code quality using strict mode, you don't need to write this over and again especially if you have multiple .js file. You can enforce this rule globally in eslint rules as follows:

    Filename: .eslintrc.js

    module.exports = {
        env: {
            es6: true
        },
        rules : {
            strict: ['error', 'global'],
            },
        };
        
    

    Okay, so what is prevented in strict mode?

    • Using a variable without declaring it will throw an error in strict mode. This is to prevent unintentionally creating global variables throughout your application. The example with printing Chicago covers this in particular.

    • Deleting a variable or a function or an argument is a no-no in strict mode.

      "use strict";
       function x(p1, p2) {}; 
       delete x; // This will cause an error
      
    • Duplicating a parameter name is not allowed in strict mode.

       "use strict";
       function x(p1, p1) {};   // This will cause an error
      
    • Reserved words in the Javascript language are not allowed in strict mode. The words are implements interface, let, packages, private, protected, public. static, and yield

    For a more comprehensive list check out the MDN documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

提交回复
热议问题