简体   繁体   English

以下代码片段中 for(;;) 的目的是什么?

[英]What is the purpose of for(;;) in the following code snippet?

So, Iwas reading through some Javascript Question answers on CodeGolf to try and improve my understanding of Javascript a lttle bit, & noticed this answer in the following question Generate Green Spraypaint Patterns Within the Answer I spotted a for loop that..[to me] is not the usual Syntax, & no matter how much I stare at it I can't figure out what this does.所以,我正在阅读 CodeGolf 上的一些 Javascript 问题答案,以尝试提高我对 Javascript 的理解,并在以下问题中注意到这个答案在答案中生成绿色喷漆模式我发现了一个 for 循环......[对我来说]不是通常的语法,无论我盯着它看多少,我都无法弄清楚它的作用。 I can't find any referance to an operator ;;我找不到对操作员的任何参考;; on Mozilla's Javascript Operators page & was wondering if anyone can explain What the double semi-colon does in the code below?在 Mozilla 的Javascript Operators页面上&想知道是否有人可以解释下面代码中双分号的作用?

b.onclick = async function() {
  b.disabled = true;
  try {
    await f(c, +s.value, +n.value, +m.value, JSON.parse(d.value));
  } catch (ex) {
    console.error(ex);
  }
  b.disabled = false;
}
async function f(canvas, size, number, moves, deltas) {
  canvas.width = canvas.height = size;
  let context = canvas.getContext('2d', { alpha: false });
  context.fillRect(0, 0, size, size);
  let image = context.getImageData(0, 0, size, size);
  for (let i = 0; i < number; i++) {
    let x = size / 2 | 0, y = x, j = moves;
    for (;;) {
      image.data[(x + y * size) * 4 + 1]++;
      if (!j--) break;
      let valid = deltas.map(([dx, dy]) => [x + dx, y + dy]).filter(([x, y]) => x >= 0 && y >= 0 && x < size && y < size);
      if (!valid.length) break;
      [x, y] = valid[valid.length * Math.random() | 0];
    }
    context.putImageData(image, 0, 0);
    await new Promise(requestAnimationFrame);
  }
}

<div>Size: <input id=s type=number min=1 value=250></div>
<div>Pixels: <input id=n type=number min=0 value=800></div>
<div>Moves: <input id=m type=number min=0 value=7000></div>
<div>Deltas: <input id=d value=[[1,2],[-1,2],[1,-2],[-1,-2],[2,1],[-2,1],[2,-1],[-2,-1]]></div>
<div><input id=b type=button value=Go!></div>
<canvas id=c>

Might be really simple, but just curious to learn.可能真的很简单,但只是好奇地学习。 Is it just a placeholder to grab the exact same info from the for loop that its nested with-in?它只是一个占位符,可以从嵌套的 for 循环中获取完全相同的信息吗?

Just for anyone else's reference, thats beginning or learning Javascript none of the below work.. lol仅供其他人参考,那是开始或学习 Javascript 下面没有一个工作..大声笑在此处输入图片说明

for(;;) is a for loop declaration with: for(;;)是一个for循环声明:

  • no initialization code没有初始化代码
  • no end-of-block expression没有块结束表达式
  • no terminating condition无终止条件

So, it's equivalent to while(true) - it'll loop forever until a break inside it is encountered.因此,它等效于while(true) - 它会一直循环直到遇到内部break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM