关于javascript:node.js中的”use strict”语句是如何解释的?

How is the 'use strict' statement interpreted in Node.js?

本问题已经有最佳答案,请猛点这里访问。

我已经开始探索node.js,编写了很多演示Web应用程序,了解node.js、express.js、jade等的流程。

但最近我遇到的一件事是,在每个函数和每个.js文件中,语句"use strict"作为第一行。

node.js如何解释它?


"use strict";

基本上它启用了严格模式。

Strict Mode is a feature that allows you to place a program, or a function, in a"strict" operating context. In strict operating context, the method form binds this to the objects as before. The function form binds this to undefined, not the global set objects.

根据你的评论,你说会有一些不同。但这是你的假设。node.js代码只是您的javascript代码。所有node.js代码都由V8 JavaScript引擎解释。V8 Javascript引擎是Google为Chrome Web浏览器开发的开源Javascript引擎。

因此,chrome浏览器和node.js对"use strict";的解释不会有重大差异。

请阅读javascript中的严格模式。

更多信息:

  • 严格模式
  • 浏览器中的ECMAScript 5严格模式支持
  • 严格的模式正在进城
  • 严格模式兼容表
  • 堆栈溢出问题:在javascript中"使用严格"做什么?背后的原因是什么?

  • ECMAScript 6:

    ECMAScript 6代码和严格模式。本规范简要说明如下:

    10.2.1 Strict Mode Code

    An ECMAScript Script syntactic unit may be processed using either unrestricted or strict mode syntax and semantics. Code is interpreted as strict mode code in the following situations:

    • Global code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive (see 14.1.1).
    • Module code is always strict mode code.
    • All parts of a ClassDeclaration or a ClassExpression are strict mode code.
    • Eval code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive or if the call to eval is a direct eval (see 12.3.4.1) that is contained in strict mode code.
    • Function code is strict mode code if the associated FunctionDeclaration, FunctionExpression, GeneratorDeclaration, GeneratorExpression, MethodDefinition, or ArrowFunction is contained in strict mode code or if the code that produces the value of the function’s [[ECMAScriptCode]] internal slot begins with a Directive Prologue that contains a Use Strict Directive.
    • Function code that is supplied as the arguments to the built-in Function and Generator constructors is strict mode code if the last argument is a String that when processed is a FunctionBody that begins with a Directive Prologue that contains a Use Strict Directive.

    此外,如果您对当前版本的node.js支持哪些功能感到迷茫,那么node.green可以帮助您(利用与kangax相同的数据)。