admin管理员组文章数量:1355713
I was poking around some HTML5 Javascript demos and came across something I've never seen before in the syntax. Take a look at the run function and notice how the search object notation is made in the while loop. Lines of interest include 15 and 18. Can anyone explain this syntax?
function run() {
var n = 1;
search: while (running) {
n += 1;
for (var i = 2; i <= Math.sqrt(n); i += 1)
if (n % i == 0)
continue search;
// found a prime!
postMessage(n);
}
}
(code taken from here; .js)
I was poking around some HTML5 Javascript demos and came across something I've never seen before in the syntax. Take a look at the run function and notice how the search object notation is made in the while loop. Lines of interest include 15 and 18. Can anyone explain this syntax?
function run() {
var n = 1;
search: while (running) {
n += 1;
for (var i = 2; i <= Math.sqrt(n); i += 1)
if (n % i == 0)
continue search;
// found a prime!
postMessage(n);
}
}
(code taken from here; http://html5demos./js/cruncher.js)
Share Improve this question edited Mar 14, 2012 at 13:41 Matt 75.3k26 gold badges156 silver badges180 bronze badges asked Mar 14, 2012 at 13:33 MattMatt 6,42210 gold badges57 silver badges85 bronze badges 5- Here you go stackoverflow./questions/418799/what-does-do-in-javascript Useth thy Google and thou shalt find! – Henrik Andersson Commented Mar 14, 2012 at 13:35
- 1 @limelights that post is about the more mon use for the colon, which doesn't happen to be the case here – tybro0103 Commented Mar 14, 2012 at 13:39
- the % in line 18 is a modulus – tybro0103 Commented Mar 14, 2012 at 13:40
- Although reading the entire post it'll be revealed that the ":" operator is also a label for something, I thought it would kill two birds with one stone. – Henrik Andersson Commented Mar 14, 2012 at 13:41
- i honestly didn't know javascript had labels like this. i am kind of glad i didn't... – jbabey Commented Mar 14, 2012 at 13:42
4 Answers
Reset to default 6This is not object (literal) notation, it is defining a label
.
A label
can be used to give a looping construct a name. The benefits of doing this is that you can create more powerful breaks;
or continues;
by referencing outer loops (by their labels).
Note that how the structure of the program you referenced is a:
search: while () {
for (;;;) {
}
}
... and the author is using continue search;
inside the for
loop to continue the execution of the while loop.
As for what's happening on line 18, if (n % i == 0)
is using the modulo (%
) operator to get the remainder between dividing n / i
, and checking whether it's 0.
search:
is a label in this case that you can use to refer to this loop.
For example you can break this loop by doing break search;
Since no one answered both your questions.
search: while
, here search
is a label which helps uniquely identify the while loop, which as mentioned, helps when using break
and/or continue
within nested loops.
Line 18 (n % i ===0)
Is basically looking for an odd number by applying the modulo operator.
This is a label, mainly used in nested loops to break/continue a certain loop that is marked by this label. This is a standard in every programming language and not javascript specific. Read more here in section "Using Labels to Control the Flow"
http://www.tutorialspoint./javascript/javascript_loop_control.htm
本文标签: htmlColon within a Javascript function while loopStack Overflow
版权声明:本文标题:html - Colon within a Javascript function while loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744044580a2581288.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论