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

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

    The statement "use strict"; instructs the browser to use the Strict mode, which is a reduced and safer feature set of JavaScript.

    List of features (non-exhaustive)

    1. Disallows global variables. (Catches missing var declarations and typos in variable names)

    2. Silent failing assignments will throw error in strict mode (assigning NaN = 5;)

    3. Attempts to delete undeletable properties will throw (delete Object.prototype)

    4. Requires all property names in an object literal to be unique (var x = {x1: "1", x1: "2"})

    5. Function parameter names must be unique (function sum (x, x) {...})

    6. Forbids octal syntax (var x = 023; some devs assume wrongly that a preceding zero does nothing to change the number.)

    7. Forbids the with keyword

    8. eval in strict mode does not introduce new variables

    9. Forbids deleting plain names (delete x;)

    10. Forbids binding or assignment of the names eval and arguments in any form

    11. Strict mode does not alias properties of the arguments object with the formal parameters. (i.e. in function sum (a,b) { return arguments[0] + b;} This works because arguments[0] is bound to a and so on. )

    12. arguments.callee is not supported

    [Ref: Strict mode, Mozilla Developer Network]

提交回复
热议问题