admin管理员组文章数量:1335854
Following code block executed successfully. I was wondering what would be the use of this labeling other than using for loops?
<script>
js:
{
alert("x");
}
</script>
Following code block executed successfully. I was wondering what would be the use of this labeling other than using for loops?
<script>
js:
{
alert("x");
}
</script>
Share
Improve this question
edited Apr 25, 2013 at 14:06
6502
115k16 gold badges174 silver badges274 bronze badges
asked Apr 25, 2013 at 14:03
KuzeyKuzey
8792 gold badges7 silver badges9 bronze badges
3
- 8 That's a colon :P – Lews Therin Commented Apr 25, 2013 at 14:05
- 3 Is this the actual and full code? – Ja͢ck Commented Apr 25, 2013 at 14:05
- 4 hth james.padolsey./javascript/labelled-blocks-useful – Fatmuemoo Commented Apr 25, 2013 at 14:09
5 Answers
Reset to default 6The :
has a few uses in javascript, that I know of anyway.
ternary operator
- used to evaluate anif
statement in a single line:var x = "yes" == "yes" ? true : false;
The above line of code is functionally equivalent to:
if("yes" == "yes"){ var x = true; } else{ var x = false; }
Mark the beginning of a code block - Move to a block of code
begin: for(int i = 0; i < 10; i++){ break begin; }
Object Literals - Thanks @Ian for the reminder
var someObject= { item: 'some value', anotherItem: 2 // Can put any type of variable here };
This type of notation is monly seen when using JSON
It's a labeled statement. You can use labeled statements with a form of the break
and continue
statements:
outer: for (var i = 0; i < 1000; ++i) {
for (var j = 0; j < 1000; j++) {
if (somethingBad())
break outer;
}
}
It's (rarely) useful to be able to get out of an inner nested loop to an outer iteration level. I don't think I've ever used it across many thousands of lines of code. In the example code posted in the original question, the label has no apparent purpose.
It is called Labels [MDN] in javascript
myPrettyLabel:
{
alert('testMe');
}
Provides a statement with an identifier that you can refer to using a break or continue statement.
For example, you can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution.
Same as goto
in general programs
Please note
Avoid using labels as Labels are not very monly used in JavaScript since they make programs harder to read and understand. As much as possible, avoid using labels and, depending on the cases, prefer calling functions or throwing an error.
Reference: SO - What does the colon (:) in JavaScript represent?
This is how you mark labels, a very bad practice which give the ability to implement the old 'goto', which is simple jump to code in sequential executing
The colon is used to add a label. As explained by the MDN documentation for label:
Provides a statement with an identifier that you can refer to using a break or continue statement.
For example, you can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution.
MDN includes a code example as well:
var i, j;
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;
} else {
console.log("i = " + i + ", j = " + j);
}
}
}
// Output is:
// "i = 0, j = 0"
// "i = 0, j = 1"
// "i = 0, j = 2"
// "i = 1, j = 0"
// "i = 2, j = 0"
// "i = 2, j = 1"
// "i = 2, j = 2"
// Notice how it skips both "i = 1, j = 1" and "i = 1, j = 2"
Also, as the article states, this kind of logic generally makes your code harder to understand, so you are better off restructuring the code to use other types of flow control (functions, etc) to avoid having to use labels.
Note that this syntax might be confusing since it is also similar to the object syntax, for example:
{ js: {1}, ... }
本文标签: What is the use of the following colon in javascriptStack Overflow
版权声明:本文标题:What is the use of the following colon in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742398021a2467317.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论