javascript 标签_JavaScript标签

javascript 标签

No matter how long you've been a JavaScript developer, there will always be language features that you didn't know about until you saw them in a fringe piece of code. Your reaction generally is a bit like:

不管您是一名JavaScript开发人员已有多久了,总会有一些您不了解的语言功能,直到您在一段附带的代码中看到它们为止。 您的React通常有点像:

Thinking

One of those features I see developers quizically trying to figure out is JavaScript labels, which allow you to manage break and continue with JavaScript loops. Let's have a look at them!

我看到开发人员奇怪地想找出的功能之一就是JavaScript标签,它允许您管理breakcontinue执行JavaScript循环。 让我们看看他们!

The basic format of a loop is {loopname}: before the loop starts:

循环的基本格式为{loopname}:循环开始前:

{loopName}:
for({iterating}) {
  {actions}
}

The power of labels comes with nested loops -- you can use break and continue, paired with the label name, to manage loop escaping:

标签的功能带有嵌套循环-您可以使用breakcontinue (与标签名称配对)来管理循环转义:


function gogogo() {
  firstLoop:
  for (let outer = 0; outer < 4; outer++) {
    secondLoop:
    for (let inner = 0; inner < 5; inner++) {
      if (inner === 3) {
        // Use continue to avoid runs 4 and 5
        continue firstLoop;
      }
      console.warn(`outer: ${outer}; inner: ${inner}`);
    }
  }
}

/*
outer: 0; inner: 0
outer: 0; inner: 1
outer: 0; inner: 2
outer: 1; inner: 0
outer: 1; inner: 1
outer: 1; inner: 2
outer: 2; inner: 0
outer: 2; inner: 1
outer: 2; inner: 2
outer: 3; inner: 0
outer: 3; inner: 1
outer: 3; inner: 2
*/

Nested loops can be difficult to manage but labels make directing and escaping them easy. The next time you want to look like a smart one in the room, break out the JavaScript labels!

嵌套循环可能很难管理,但是标签使定向和转义变得容易。 下次您想在房间里看起来像个聪明人时,突破JavaScript标签!

翻译自: https://davidwalsh.name/javascript-labels

javascript 标签

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值