首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在其他块中,这些额外的花括号的用途是什么?

在其他块中,这些额外的花括号的用途是什么?
EN

Stack Overflow用户
提问于 2022-07-19 21:08:45
回答 1查看 46关注 0票数 0

我正在查看一些类型代码,我看到在一个其他块中使用了额外的花括号。这些额外的支架有什么用处吗?还是简单地将额外的分组添加到两个几乎相同的代码片段中?鉴于这是一个非常流行的微软回购,它怀疑它的意外。

代码语言:javascript
复制
  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);
      });
    }
  }
}

来源

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-19 21:26:12

它只是为变量创建新范围的块。您可以看到const command = ... --这不可能在一个范围内,否则会引发错误:SyntaxError: redeclaration of const command

代码语言:javascript
复制
// 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等。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73043491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档