我在代码中看到了这种模式,我试图理解它背后的想法
foo : {
//some code here
console.log('1');
}
foo 是一些 html 节点,console.log 代表一些代码。我看到 console.log 始终执行,并且我认为没有理由使用此模式,而不仅仅是直接写入 console.log 。
您还可以看到,如果我在 undefined variable 上使用它,它就会起作用。
this_does_not_exists : {
console.log(2);
}
有人使用这种模式的原因是什么?
最佳答案
这是一个label并且与变量无关。这些通常仅在处理 while
或 for
循环(当您想要break
或 continue
时有用)比最里面的那个。
来自 MDN 的示例:
loop1:
for (i = 0; i < 3; i++) { //The first for statement is labeled "loop1"
loop2:
for (j = 0; j < 3; j++) { //The second for statement is labeled "loop2"
if (i == 1 && j == 1) {
continue loop1;
}
console.log("i = " + i + ", j = " + j);
}
}
在此示例中,代码从内循环中继续
外循环。
关于javascript - 这个 JavaScript 语法/模式是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23732407/