“use strict”在 Node 版本 14 中有用吗?

Cem*_*aan 7 node.js

我已从Node.js 12.19更新到Node.js 14.15

使用这条线还有什么区别吗use strict

它能保证最新的功能吗?

Rya*_*ton 0

我找不到有关use strict某个版本是否启用该模式的任何详细文档。但我使用的是 Node v16.20.1(type: "module"在我的 package.json 中),我发现这篇博客文章给了我一个很好的测试,我运行它告诉我在设置时至少在 Node 16 中肯定use strict默认启用的。type: module

我在项目中创建了 2 个测试文件:

测试.js:

'use strict';
import mod from './myModule.js';

function local(arg) {
  console.log('Local');
  console.log(`${arg} -- ${arguments[0]}`);
  arguments[0] = 20;
  console.log(`${arg} -- ${arguments[0]}`);
}

local(10);
mod(10);
Run Code Online (Sandbox Code Playgroud)

myModule.js

export default function(arg) {
  console.log('Mod')
  console.log(`${arg} -- ${arguments[0]}`);
  arguments[0] = 20;
  console.log(`${arg} -- ${arguments[0]}`);
}
Run Code Online (Sandbox Code Playgroud)

我运行时的输出是:

C:\myproject> node ./test.js
Local
10 -- 10
10 -- 20
Mod
10 -- 10
10 -- 20
Run Code Online (Sandbox Code Playgroud)

如果您运行此测试并且它显示20 -- 20为最后一行,那么您就知道您需要use strict在文件顶部启用严格模式。如果是10 -- 20,则默认启用。