我已经开始探索Node.js,并编写了许多演示web应用程序,了解Node.js, Express.js, jade等流程。

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

Node.js是如何解释它的?


“使用严格的”;

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

严格模式是一种允许您将程序或函数置于“严格”操作上下文中的特性。在严格的操作上下文中,方法形式像以前一样将其绑定到对象。函数形式将this绑定到undefined,而不是全局set对象。

根据你的评论,你说会有一些差异。但这只是你的假设。Node.js代码就是你的JavaScript代码。所有Node.js代码都由V8 JavaScript引擎解释。V8 JavaScript引擎是谷歌为Chrome浏览器开发的开源JavaScript引擎。

所以,如何“严格使用”并没有太大的区别;由Chrome浏览器和Node.js解释。

请阅读JavaScript中什么是严格模式。

欲了解更多信息:

严格模式 ECMAScript 5浏览器中的严格模式支持 严格模式即将到来 严格模式兼容性表 Stack Overflow问题:“use strict”在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. js。Green可以帮助您(利用与kangax相同的数据)。