Node.js 中如何解释“use strict”语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18417381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How is the 'use strict' statement interpreted in Node.js?
提问by Amol M Kulkarni
I have started to explore the Node.js and wrote many demo web application, to understand the flow of Node.js, Express.js, jade, etc..
我已经开始探索 Node.js 并编写了许多演示 Web 应用程序,以了解 Node.js、Express.js、jade 等的流程。
But one thing I came across recently, is the statement "use strict"as the first line inside every function and every .jsfile.
但是我最近遇到的一件事是将语句"use strict"作为每个函数和每个.js文件中的第一行。
How exactly is it is interpreted by Node.js?
Node.js 究竟是如何解释它的?
回答by Amol M Kulkarni
"use strict";
"use strict";
Basically it enables the strict mode.
基本上它启用严格模式。
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.
严格模式是一项功能,允许您将程序或函数置于“严格”操作上下文中。在严格的操作上下文中,方法形式像以前一样将 this 绑定到对象。函数形式将 this 绑定到 undefined,而不是全局集合对象。
As per your comments you are telling some differences will be there. But it's your assumption. The Node.js code is nothing but your JavaScript code. All Node.js code are interpreted by the V8JavaScript engine. The V8 JavaScript Engineis an open source JavaScript engine developed by Google for Chrome web browser.
根据您的评论,您告诉我们会有一些差异。但这是你的假设。Node.js 代码只不过是您的 JavaScript 代码。所有 Node.js 代码都由V8JavaScript 引擎解释。在V8 JavaScript引擎是由谷歌的Chrome网络浏览器开发的开源JavaScript引擎。
So, there will be no major difference how "use strict";is interpreted by the Chrome browser and Node.js.
因此,"use strict";Chrome 浏览器和 Node.js 的解释方式不会有太大区别。
Please read what is strict mode in JavaScript.
请阅读 JavaScript 中的严格模式。
For more information:
想要查询更多的信息:
- Strict mode
- ECMAScript 5 Strict mode support in browsers
- Strict mode is coming to town
- Compatibility table for strict mode
- Stack Overflow questions: what does 'use strict' do in JavaScript & what is the reasoning behind it
ECMAScript 6:ECMAScript 6:
ECMAScript 6 Code & strict mode. Following is brief from the specification:
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.
10.2.1 严格模式代码
ECMAScript Script 语法单元可以使用不受限制或严格模式的语法和语义进行处理。在以下情况下,代码被解释为严格模式代码:
- 如果全局代码以包含使用严格指令的指令序言开头(参见 14.1.1),则它是严格模式代码。
- 模块代码始终是严格模式代码。
- ClassDeclaration 或 ClassExpression 的所有部分都是严格模式代码。
- 如果 Eval 代码以包含使用严格指令的指令序言开头,或者如果对 eval 的调用是包含在严格模式代码中的直接 eval(参见 12.3.4.1),则它是严格模式代码。
- 如果相关的 FunctionDeclaration、FunctionExpression、GeneratorDeclaration、GeneratorExpression、MethodDefinition 或 ArrowFunction 包含在严格模式代码中,或者产生函数 [[ECMAScriptCode]] 内部槽值的代码以指令序言开头,则函数代码是严格模式代码包含使用严格指令。
- 如果最后一个参数是一个字符串,则作为参数提供给内置函数和生成器构造函数的函数代码是严格模式代码,当处理时它是一个以包含使用严格指令的指令序言开头的函数体。
Additionally if you are lost on what features are supported by your current version of Node.js, this node.greencan help you (leverages from the same data as kangax).
此外,如果您不知道当前版本的 Node.js 支持哪些功能,这个node.green可以帮助您(利用与kangax相同的数据)。

