为什么这个标记为 javaScript continue 不起作用?

发布于 2024-11-16 07:06:55 字数 515 浏览 3 评论 0原文

我使用这段代码来应对一些圆圈重叠:

iCantThinkOfAGoodLabelName:
x = genX(radius);
y = genY(radius);
for(i in circles) {
  var thisCircle = circles[i];
  if(Math.abs(x-thisCircle["x"])+Math.abs(y-thisCircle["y"])>radius*2) { //No overlap
    continue;
  } else { //Overlap
    continue iCantThinkOfAGoodLabelName; //<- Line 256
  }
  thisCircle = [];
}

但是当到达 continue 语句时,chrome 的开发者控制台给我这个: client.html:256 Uncaught SyntaxError: Undefined label 'iCantThinkOfAGoodLabelName'

I am using this code to weather some circles are overlapping:

iCantThinkOfAGoodLabelName:
x = genX(radius);
y = genY(radius);
for(i in circles) {
  var thisCircle = circles[i];
  if(Math.abs(x-thisCircle["x"])+Math.abs(y-thisCircle["y"])>radius*2) { //No overlap
    continue;
  } else { //Overlap
    continue iCantThinkOfAGoodLabelName; //<- Line 256
  }
  thisCircle = [];
}

But when the continue statement is reached, chrome's developer console gives me this: client.html:256 Uncaught SyntaxError: Undefined label 'iCantThinkOfAGoodLabelName'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瘫痪情歌 2024-11-23 07:06:55

标签应该紧接在循环之前

x = genX(radius);
y = genY(radius);

iCantThinkOfAGoodLabelName:
for(i in circles) {

The label should come immediately before the loop

x = genX(radius);
y = genY(radius);

iCantThinkOfAGoodLabelName:
for(i in circles) {
梦初启 2024-11-23 07:06:55

因为 iCantThinkOfAGoodLabelName: 需要位于循环之前。

iCantThinkOfAGoodLabelName:
for (blah; blah; blah)
    ..

我认为你想要的是一个功能..

function iCantThinkOfAGoodFunctionName() {
    var x = genX(radius),
        y = genY(radius);

    for (i in circles) {
        var thisCircle = circles[i];
        if(Math.abs(x-thisCircle["x"])+Math.abs(y-thisCircle["y"])>radius*2) { //No overlap
            continue;
        } else { //Overlap
            iCantThinkOfAGoodFunctionName();
        }
        thisCircle = [];
    }
}

Because iCantThinkOfAGoodLabelName: needs to be right before the loop.

iCantThinkOfAGoodLabelName:
for (blah; blah; blah)
    ..

I think what you want is a function..

function iCantThinkOfAGoodFunctionName() {
    var x = genX(radius),
        y = genY(radius);

    for (i in circles) {
        var thisCircle = circles[i];
        if(Math.abs(x-thisCircle["x"])+Math.abs(y-thisCircle["y"])>radius*2) { //No overlap
            continue;
        } else { //Overlap
            iCantThinkOfAGoodFunctionName();
        }
        thisCircle = [];
    }
}
老街孤人 2024-11-23 07:06:55

标签名称和关联循环之间不应有任何语句。

x = genX(radius);
y = genY(radius);
iCantThinkOfAGoodLabelName:
    for(i in circles) {

修复它。

There should not be any statement between a label name and associated loop.

x = genX(radius);
y = genY(radius);
iCantThinkOfAGoodLabelName:
    for(i in circles) {

fixes it.

泛滥成性 2024-11-23 07:06:55

我最近遇到了这个问题,我通过在 Node.js 版本 v0.8.x 的循环标签中使用全小写来解决它。

使用 labelname:iCantThinkOfAGoodLabelName: 可能会对您有所帮助。

其他人已经正确纠正了您标签的位置。它应该紧接在 for 循环之前。

标签上的 Mozilla 开发者网络 建议避免使用标签,而更喜欢调用函数抛出错误。如果可能的话,您可能会重新考虑使用它们的策略。

根据结果​​调用函数的示例:

var i, j;

for (i = 0; i < 3; i++) {
   for (j = 0; j < 3; j++) {   
      if (i == 1 && j == 1) {
         // we want to ignore and continue
      } else {
         // do work with these variables from inside the doWork function
         // (scoped to whatever scope `this` for loop is in)
         doWork.call(this, i, j); 
      }
   }
}

I recently had this issue and I resolved it by using all lowercase in the loop's label in version v0.8.x of Node.js.

Using labelname: vs. iCantThinkOfAGoodLabelName: might help you.

Others have correctly corrected you on the location of the label. It should be immediately before the for loop.

The Mozilla Developer Network on labels advises to avoid using labels, and instead prefer calling functions or throwing an error. You might rethink your strategy on using them, if possible.

Example of calling a function depending on result:

var i, j;

for (i = 0; i < 3; i++) {
   for (j = 0; j < 3; j++) {   
      if (i == 1 && j == 1) {
         // we want to ignore and continue
      } else {
         // do work with these variables from inside the doWork function
         // (scoped to whatever scope `this` for loop is in)
         doWork.call(this, i, j); 
      }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文