如何在Node.js中解释'use strict'语句? [英] How is the 'use strict' statement interpreted in Node.js?

查看:355
本文介绍了如何在Node.js中解释'use strict'语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



<但是我最近遇到的一件事是语句use strict作为每个函数内的第一行和每个 .js file。



Node.js究竟解释了什么?

解决方案

use strict;



基本上它启用严格模式。


严格模式是一种允许您在严格操作环境中放置程序或函数的功能。在严格的操作上下文中,方法表单像以前一样将它绑定到对象。函数形式将其绑定到未定义,而不是全局集对象。


根据您的评论,您会发现存在一些差异。但这是你的假设。 Node.js代码只是你的JavaScript代码。所有Node.js代码都由 V8 JavaScript引擎解释 V8 JavaScript引擎是Google为Chrome网络浏览器开发的一个开源JavaScript引擎。



所以,没有什么重大区别use strict; 由Chrome浏览器和Node.js解释。



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



有关详细信息:


  1. 严格模式

  2. ECMAScript 5严格的模式支持浏览器

  3. 严格的模式即将来到城镇

  4. 严格的兼容性表模式

  5. Stack Overflow问题:JavaScript中使用'严格'的做法&背后的原因是什么?



ECMAScript 6:



ECMAScript 6 Code&严格模式。以下是规范的简要说明:


10.2.1严格模式代码



可以使用无限制或严格模式语法和语义来处理ECMAScript脚本语法单元。在以下情况下,代码被解释为严格模式代码:




  • 全局代码是严格模式代码,如果它以包含a的指令序言开头使用严格指令(见14.1.1)。

  • 模块代码始终是严格模式代码。

  • 所有部分ClassDeclaration或ClassExpression是严格模式代码。

  • 如果Eval代码以包含Use Strict Directive的Directive Prologue开头,或者对eval的调用是直接eval,则Eval代码是严格模式代码(见12.3.4.1)包含在严格模式代码中。

  • 如果严格包含关联的FunctionDeclaration,FunctionExpression,GeneratorDeclaration,GeneratorExpression,MethodDefinition或ArrowFunction,则函数代码是严格模式代码模式代码或者如果产生函数[[ECMAScriptCode]]内部插槽值的代码以包含Use Strict Directive的指令序言开头。

  • 作为内置函数和生成器构造函数的参数提供的函数代码是严格模式代码,如果最后一个参数是一个String,处理时是一个以包含Use Strict指令的指令序言开头的FunctionBody。


此外,如果您丢失了当前版本的Node.js支持的功能,这个 node.green 可以帮助您(利用与 kangax )。


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..

But one thing I came across recently, is the statement "use strict" as the first line inside every function and every .js file.

How exactly is it is interpreted by Node.js?

解决方案

"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.

So, there will be no major difference how "use strict"; is interpreted by the Chrome browser and Node.js.

Please read what is strict mode in JavaScript.

For more information:

  1. Strict mode
  2. ECMAScript 5 Strict mode support in browsers
  3. Strict mode is coming to town
  4. Compatibility table for strict mode
  5. Stack Overflow questions: what does 'use strict' do in JavaScript & what is the reasoning behind it


ECMAScript 6:

ECMAScript 6 Code & strict mode. Following is brief from the specification:

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.

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).

这篇关于如何在Node.js中解释'use strict'语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆