node.js 有什么方法可以在节点中强制使用严格模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9031888/
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
Any way to force strict mode in node?
提问by Robin Heggelund Hansen
Could not find this answer anywhere, but I did find several mailing lists where this was discussed, these are rather old however and I have no idea if this is implemented or not.
在任何地方都找不到这个答案,但我确实找到了几个讨论这个问题的邮件列表,但是这些已经很老了,我不知道这是否已实现。
Is there anyway to force using strict mode in node.js?
无论如何要强制在 node.js 中使用严格模式?
Writing "use strict";in all my .js files... well, i prefer it being forced to using strict mode, rather than adding extra boilerplate.
写入"use strict";我所有的 .js 文件......好吧,我更喜欢强制使用严格模式,而不是添加额外的样板。
回答by Chad Scira
According to Lloyd you can now place
根据劳埃德的说法,您现在可以放置
"use strict";
at the top of your file in node >= 0.10.7, but if you want your whole app to run in strict (including external modules) you can do this
在node >= 0.10.7文件的顶部,但是如果您希望整个应用程序严格运行(包括外部模块),您可以执行此操作
node --use_strict
节点 --use_strict
回答by Lloyd
In node 0.10.7 you can enforce strict mode at file level by placing "use strict";at the top of your file. Finally!
在节点 0.10.7 中,您可以通过放置"use strict";在文件顶部来在文件级别强制执行严格模式。最后!
回答by Gianluca Casati
You can also use
你也可以使用
https://npmjs.org/package/use-strict
https://npmjs.org/package/use-strict
that is, write once
也就是说,写一次
require('use-strict')
or even take a step forward and use
甚至向前迈出一步并使用
https://npmjs.org/package/node-strict
https://npmjs.org/package/node-strict
Please note that use-strictwill turn on strict more on every module required after invocation.
请注意,use-strict调用后将在每个所需的模块上更严格地打开。
If you prefer a not invasive approach, I wrote another module
如果你更喜欢非侵入性的方法,我写了另一个模块
https://www.npmjs.org/package/strict-mode
https://www.npmjs.org/package/strict-mode
which enables strict mode only in your package. I think that is more a "Do What I Mean" solution.
仅在您的包中启用严格模式。我认为这更像是“按我的意思做”的解决方案。
回答by Evan Carroll
Just use "use strict";at the top of applicable files. I know it's tempting to try to cut out boilerplate, but it simply can not be done in Javascript. The node flag which shall not be named[1]
只需"use strict";在适用文件的顶部使用。我知道尝试删除样板文件很诱人,但它根本无法在 Javascript 中完成。不应命名的节点标志[1]
- is undocumented, and unsupported by Node itself.
- has faced proposals to remove it.
- is node-specific and is not supported in any other JavaScript engine.
- is unstandardized.
- it is notthe same as
"use strict";because it is a compiler global, and like all globals you're potentially adversely impacting someone else's code. - everything is subject to bugs. strict mode and sloppy-mode may be subject to different bugs. that is to say, some strict mode bugs are unique to strict mode
- 没有记录,并且不受 Node 本身的支持。
- 已经面临删除它的建议。
- 是特定于节点的,不受任何其他 JavaScript 引擎支持。
- 是不规范的。
- 它是不一样的
"use strict";,因为它是一个编译器全球性的,像所有的全局,您就有可能造成不利影响别人的代码。 - 一切都受到错误的影响。严格模式和马虎模式可能会遇到不同的错误。也就是说,一些严格模式的bug是严格模式独有的
Some other programmers may think this is similar to -wALLor the like, it's not. This is standardized functionality that you're enabling in an ad-hoc fashion (breaking the standard) and changing everyone's compiler semantics.
其他一些程序员可能认为这类似于-wALL什么,其实不然。这是您以临时方式(打破标准)启用并更改每个人的编译器语义的标准化功能。
Footnotes
脚注
- The node flag is
--use_strict. Don't use it.
- 节点标志是
--use_strict。不要使用它。
回答by John Lehmann
You can also provide the strict flag on the shebang interpreter directive.
您还可以在 shebang 解释器指令上提供严格标志。
#!/usr/bin/env node --use_strict
But currently (at least pre v0.9.x) it suffers the same problems described by the comments in @chad-scira's answer discuss.
但是目前(至少在 v0.9.x 之前)它遇到了@chad-scira 的回答讨论中的评论所描述的相同问题。

