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

前端 未结 28 3657
半阙折子戏
半阙折子戏 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条回答
  •  萌比男神i
    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

提交回复
热议问题