In JavaScript version 1.2, the case and default labels were added, which introduced as a by-product the fact that any fragment of code can be labelled with an identifier.
The identifier can be any legal JavaScript identifier that does not match a reserved keyword.
The namespace that labels exist in, is separate to that of variables and function names. This means you can use the same identifier names over again, although it's probably good practice not to.
Adding a label in front of an iterator allows you to associate a label name with a break or continue statement. This means that you can break or continue nested iterators from deep inside them. This can greatly simplify the logic of a looping system.
If the goto keyword is ever implemented, it would depend on this labelling mechanism being extended to provide a useful destination.
// An example break to a labelled line outerLoop: for (var i=0; i <= max; i++) { for (var j=0; j <= max2; j++) { if (i== someNum && j ==someNum2) { break outerLoop; } } }
Prev | Home | Next |
L | Up | Label object |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |