What is “strict mode” and how is it used?

前端 未结 8 2206
南旧
南旧 2020-12-02 05:42

I\'ve been looking over the JavaScript reference on the Mozilla Developer Network, and I came across something called "strict mode". I read it over an

8条回答
  •  遥遥无期
    2020-12-02 05:53

    2017 and I finally found the documentation:
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

    Strict mode is a way to opt in to a restricted variant of JavaScript. Strict mode isn't just a subset: it intentionally has different semantics from normal code. Browsers not supporting strict mode will run strict mode code with different behavior from browsers that do, so don't rely on strict mode without feature-testing for support for the relevant aspects of strict mode. Strict mode code and non-strict mode code can coexist, so scripts can opt into strict mode incrementally.


    Strict mode makes several changes to normal JavaScript semantics. First, strict mode eliminates some JavaScript silent errors by changing them to throw errors. Second, strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode. Third, strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.

提交回复
热议问题