简体   繁体   English

在 Visual Studio Code 中启用 JS 严格模式

[英]JS strict mode enabling in Visual Studio Code

To enable strict mode in JavaScript it's needed to insert 'use strict' to a script.要在 JavaScript 中启用严格模式,需要在脚本中插入“use strict”。 If I have several scripts, I need to add it to all.如果我有多个脚本,我需要将其添加到所有脚本中。 Maybe it can be added only once in settings?也许它只能在设置中添加一次? Not found any examples for now.目前没有找到任何例子。

PS I know about add-ons that add statement to all scripts, it's not very convenient anyway. PS 我知道有一些附加组件可以在所有脚本中添加语句,反正不是很方便。

To enable strict mode in JavaScript it's needed to insert 'use strict' to a script.要在 JavaScript 中启用严格模式,需要在脚本中插入“use strict”。

Or to use modules, which are strict by default.或者使用默认情况下严格的模块。 To tell the JavaScript parser (and VS Code) that a file is a module, add an import or export to it.要告诉 JavaScript 解析器(和 VS 代码)文件是一个模块,请向其添加importexport You can have an empty export if the file is completely standalone:如果文件是完全独立的,则可以进行空export

export {}

...although usually, once you adopt modules, you just naturally end up with import and export to share functions and such between the modules, so you don't need something synthetic like that (or "use strict"; ). ...虽然通常情况下,一旦您采用模块,您自然会以importexport结束,以在模块之间共享功能等,因此您不需要像那样的合成器(或"use strict"; )。

Modules offer benefits beyond automatically being in strict mode, such as having their own top-level scope (rather than top-level code being in global scope) and, of course, declarative dependencies between files.模块提供的好处不仅仅是自动处于严格模式,例如拥有自己的顶级 scope(而不是全局范围内的顶级代码),当然还有文件之间的声明依赖性。

More about modules on MDN and in Chapter 13 of my recent book JavaScript: The New Toys .更多关于MDN 上的模块以及我最近出版的书JavaScript:新玩具的第 13 章。

Maybe it can be added only once in settings?也许它只能在设置中添加一次?

The problem with doing that is that VS Code's settings have no effect on the JavaScript engine that will ultimately be responsible for parsing and evaluating the code.这样做的问题是 VS Code 的设置对最终负责解析和评估代码的 JavaScript 引擎没有影响。 That's why you need something in the file itself.这就是为什么您需要文件本身中的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM