Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can labels be applied to all language statements?

Tags:

javascript

According to the ECMAScript standard, labels can be applied to all statements.

This means that the following lines are valid:

myLabel: throw 'whatever'

as well as

function foo() {
  myLabel: return 'whatever'
}

console.log(foo())

But these labels appear to be unusable, which begs the question: why are they allowed? (Or maybe I have a fundamental misunderstanding)

like image 491
Ben Aston Avatar asked Nov 02 '25 17:11

Ben Aston


1 Answers

According to Brendan Eich, the author of JavaScript, this approach aped Java (the new hotness in 1996), and avoided the need for "dead label analysis":

Same as Java, IIRC. We didn't want to require engines to do dead label analysis. I know for sure I didn't want to when I first implemented in SpiderMonkey. This has led to unintended transposed javascript: labels (used to be URL schemes), but they're harmless.

like image 57
2 revs, 2 users 80%Ben Aston Avatar answered Nov 04 '25 08:11

2 revs, 2 users 80%Ben Aston