我正在查看一些类型代码,我看到在一个其他块中使用了额外的花括号。这些额外的支架有什么用处吗?还是简单地将额外的分组添加到两个几乎相同的代码片段中?鉴于这是一个非常流行的微软回购,它怀疑它的意外。
if (playwrightTestPackagePath) {
require(playwrightTestPackagePath).addTestCommand(program);
require(playwrightTestPackagePath).addShowReportCommand(program);
require(playwrightTestPackagePath).addListFilesCommand(program);
} else {
{
const command = program.command('test').allowUnknownOption(true);
command.description('Run tests with Playwright Test. Available in @playwright/test package.');
command.action(async () => {
console.error('Please install @playwright/test package to use Playwright Test.');
console.error(' npm install -D @playwright/test');
process.exit(1);
});
}
{
const command = program.command('show-report').allowUnknownOption(true);
command.description('Show Playwright Test HTML report. Available in @playwright/test package.');
command.action(async () => {
console.error('Please install @playwright/test package to use Playwright Test.');
console.error(' npm install -D @playwright/test');
process.exit(1);
});
}
}
}发布于 2022-07-19 21:26:12
它只是为变量创建新范围的块。您可以看到const command = ... --这不可能在一个范围内,否则会引发错误:SyntaxError: redeclaration of const command。
// no errors
{
const x = 1;
console.log(x) // 1
}
{
const x = 2;
console.log(x) // 2
}
{
const x = 3;
console.log(x) // 3
}
// error
const x = 1
const x = 2 // Uncaught SyntaxError: redeclaration of const x
const x = 3我认为这只是为了避免命名像command2, command3等。
https://stackoverflow.com/questions/73043491
复制相似问题