I was just browsing the source of JSLint and noticed this piece of code:
// Is this a labeled statement?
//...
if (next_token.labeled !== true || funct === global_funct) {
stop('unexpected_label_a', label);
} //...
The interesting part is the funct === global_funct comparison. Running the following snippet through JSLint throws an "Unexpected label" error, since the labelled statement is in the global execution context (I know, it's a stupid example. Here's a fiddle.):
loop:
for (var i = 0; i < 10; i++) {
if (i === 5) {
break loop;
}
}
If you place that same snippet in a function, JSLint is perfectly happy with it and doesn't throw an error when it encounters the label. Here's a fiddle with code that will pass JSLint. The code can be pasted into the online version of JSLint if you want to try it.
So my question: is there anything wrong with using a labelled statement in global code or is it just another personal choice by Crockford?