This question already has an answer here:
这个问题已经有了答案:
- What does “use strict” do in JavaScript, and what is the reasoning behind it? 26 answers
- 在JavaScript中“使用严格的”是什么?它背后的原因是什么?26日答案
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..
我已经开始探索这个节点。并编写了许多演示web应用程序,以了解节点的流程。js,表达。js、玉石等。
But one thing I came across recently, is the statement "use strict"
as the first line inside every function and every .js
file.
但是我最近遇到的一件事是,声明“使用严格”作为每个函数和每个.js文件中的第一行。
How exactly is it is interpreted by Node.js?
Node.js到底是怎么解释的?
1 个解决方案
#1
147
"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.
严格模式是允许您将程序或函数放置在“严格”操作上下文中的特性。在严格的操作上下文中,方法表单将其与对象绑定在一起。函数形式将它绑定到未定义的对象,而不是全局集对象。
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 V8 JavaScript engine. The V8 JavaScript Engine is an open source JavaScript engine developed by Google for Chrome web browser.
根据你的评论,你说会有一些不同。但这是你的假设。的节点。js代码就是你的JavaScript代码。所有节点。V8 JavaScript引擎解释了js代码。V8 JavaScript引擎是由谷歌为Chrome浏览器开发的开源JavaScript引擎。
So, there will be no major difference how "use strict";
is interpreted by the Chrome browser and Node.js.
所以,如何“使用严格”并没有太大的区别;由Chrome浏览器和Node.js解释。
Please read what is strict mode in JavaScript.
请阅读JavaScript中的严格模式。
For more information:
更多信息:
- Strict mode
- 严格模式
- ECMAScript 5 Strict mode support in browsers
- 在浏览器中,ECMAScript 5严格的模式支持。
- 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
- Stack Overflow问题:在JavaScript中“使用严格的”是什么?它背后的原因是什么?
ECMAScript 6:
ECMAScript 6 Code & strict mode. Following is brief from the specification:
ECMAScript 6代码严格模式。以下是说明书的简要说明:
10.2.1 Strict Mode Code
10.2.1严格模式代码
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:
可以使用无限制或严格的模式语法和语义来处理ECMAScript脚本语法单元。在以下情况下,代码被解释为严格的模式代码:
- Global code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive (see 14.1.1).
- 全局代码是严格的模式代码,如果它以包含使用严格指令的指令开头(参见14.1.1)。
- Module code is always strict mode code.
- 模块代码总是严格的模式代码。
- All parts of a ClassDeclaration or a ClassExpression are strict mode code.
- 类声明或ClassExpression的所有部分都是严格的模式代码。
- 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.
- Eval代码是严格的模式代码,如果它以包含使用严格指令的指令序言开始,或者如果对Eval的调用是直接包含在严格模式代码中的Eval(参见12.3.4.1)。
- 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.
- 函数代码是严格的模式代码,如果相关的FunctionDeclaration、FunctionExpression、GeneratorDeclaration、GeneratorExpression、MethodDefinition或ArrowFunction被包含在严格的模式代码中,或者产生函数值的代码[[ECMAScriptCode]]内部槽,则从包含使用严格指令的指令序开始。
- 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.
- 如果最后一个参数是一个字符串,那么作为内置函数和生成器构造函数的参数提供的函数代码就是严格的模式代码,如果最后一个参数是一个处理时以包含严格指令的指令序言开始的函数体。
Additionally if you are lost on what features are supported by your current version of Node.js, this node.green can help you (leverages from the same data as kangax).
此外,如果您丢失了当前版本的节点支持的特性。js,这个节点。绿色可以帮助你(利用与kangax相同的数据)。
#1
147
"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.
严格模式是允许您将程序或函数放置在“严格”操作上下文中的特性。在严格的操作上下文中,方法表单将其与对象绑定在一起。函数形式将它绑定到未定义的对象,而不是全局集对象。
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 V8 JavaScript engine. The V8 JavaScript Engine is an open source JavaScript engine developed by Google for Chrome web browser.
根据你的评论,你说会有一些不同。但这是你的假设。的节点。js代码就是你的JavaScript代码。所有节点。V8 JavaScript引擎解释了js代码。V8 JavaScript引擎是由谷歌为Chrome浏览器开发的开源JavaScript引擎。
So, there will be no major difference how "use strict";
is interpreted by the Chrome browser and Node.js.
所以,如何“使用严格”并没有太大的区别;由Chrome浏览器和Node.js解释。
Please read what is strict mode in JavaScript.
请阅读JavaScript中的严格模式。
For more information:
更多信息:
- Strict mode
- 严格模式
- ECMAScript 5 Strict mode support in browsers
- 在浏览器中,ECMAScript 5严格的模式支持。
- 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
- Stack Overflow问题:在JavaScript中“使用严格的”是什么?它背后的原因是什么?
ECMAScript 6:
ECMAScript 6 Code & strict mode. Following is brief from the specification:
ECMAScript 6代码严格模式。以下是说明书的简要说明:
10.2.1 Strict Mode Code
10.2.1严格模式代码
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:
可以使用无限制或严格的模式语法和语义来处理ECMAScript脚本语法单元。在以下情况下,代码被解释为严格的模式代码:
- Global code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive (see 14.1.1).
- 全局代码是严格的模式代码,如果它以包含使用严格指令的指令开头(参见14.1.1)。
- Module code is always strict mode code.
- 模块代码总是严格的模式代码。
- All parts of a ClassDeclaration or a ClassExpression are strict mode code.
- 类声明或ClassExpression的所有部分都是严格的模式代码。
- 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.
- Eval代码是严格的模式代码,如果它以包含使用严格指令的指令序言开始,或者如果对Eval的调用是直接包含在严格模式代码中的Eval(参见12.3.4.1)。
- 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.
- 函数代码是严格的模式代码,如果相关的FunctionDeclaration、FunctionExpression、GeneratorDeclaration、GeneratorExpression、MethodDefinition或ArrowFunction被包含在严格的模式代码中,或者产生函数值的代码[[ECMAScriptCode]]内部槽,则从包含使用严格指令的指令序开始。
- 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.
- 如果最后一个参数是一个字符串,那么作为内置函数和生成器构造函数的参数提供的函数代码就是严格的模式代码,如果最后一个参数是一个处理时以包含严格指令的指令序言开始的函数体。
Additionally if you are lost on what features are supported by your current version of Node.js, this node.green can help you (leverages from the same data as kangax).
此外,如果您丢失了当前版本的节点支持的特性。js,这个节点。绿色可以帮助你(利用与kangax相同的数据)。