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

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

    use strict is a way to make your code safer, because you can't use dangerous features that can work not as you expect. And, as was written before, it makes code more strict.

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

    "use strict"; Defines that JavaScript code should be executed in "strict mode".

    • The "use strict" directive was new in 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.

    All modern browsers support "use strict" except Internet Explorer 9 and lower.

    Disadvantage

    If a developer used a library that was in strict mode, but the developer was used to working in normal mode, they might call some actions on the library that wouldn’t work as expected.

    Worse, since the developer is in normal mode, they don’t have the advantages of extra errors being thrown, so the error might fail silently.

    Also, as listed above, strict mode stops you from doing certain things.

    People generally think that you shouldn’t use those things in the first place, but some developers don’t like the constraint and want to use all the features of the language.

    • For basic example and for reference go through :

      https://www.tutorialsteacher.com/javascript/javascript-strict

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

    Strict mode can prevent memory leaks.

    Please check the function below written in non-strict mode:

    function getname(){
        name = "Stack Overflow"; // Not using var keyword
        return name;
    }
    getname();
    console.log(name); // Stack Overflow
    

    In this function, we are using a variable called name inside the function. Internally, the compiler will first check if there is any variable declared with that particular name in that particular function scope. Since the compiler understood that there is no such variable, it will check in the outer scope. In our case, it is the global scope. Again, the compiler understood that there is also no variable declared in the global space with that name, so it creates such a variable for us in the global space. Conceptually, this variable will be created in the global scope and will be available in the entire application.

    Another scenario is that, say, the variable is declared in a child function. In that case, the compiler checks the validity of that variable in the outer scope, i.e., the parent function. Only then it will check in the global space and create a variable for us there. That means additional checks need to be done. This will affect the performance of the application.


    Now let's write the same function in strict mode.

    "use strict"
    function getname(){
        name = "Stack Overflow"; // Not using var keyword
        return name;
    }
    getname();
    console.log(name); 
    

    We will get the following error.

    Uncaught ReferenceError: name is not defined
    at getname (<anonymous>:3:15)
    at <anonymous>:6:5
    

    Here, the compiler throws the reference error. In strict mode, the compiler does not allow us to use the variable without declaring it. So memory leaks can be prevented. In addition, we can write more optimized code.

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

    "use strict"; is the ECMA effort to make JavaScript a little bit more robust. It brings in JS an attempt to make it at least a little "strict" (other languages implement strict rules since the 90s). It actually "forces" JavaScript developers to follow some sort of coding best practices. Still, JavaScript is very fragile. There is no such thing as typed variables, typed methods, etc. I strongly recommend JavaScript developers to learn a more robust language such as Java or ActionScript3, and implement the same best practices in your JavaScript code, it will work better and be easier to debug.

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