19 ms·
Goto can already be functionally achieved with labels, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label https://developer.mozi
by kristopolous 9y ago
Goto can already be functionally achieved with labels, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- vog 9y agoGood hint! However, JavaScript labels via break/continue have subtle differences to classic GOTO statements. From the MDN site examples: var itemsPassed = 0; var i, j; top: for (i = 0; i < items.length; i++) { for (j = 0; j < tests.length; j++) { if (!tests[j].pass(items[i])) { continue top; } } itemsPassed++; } The statement "continue top" does not really jump to top, because it only increases "i" and rechecks the loop condition. In contrast "GOTO top" would re-initialize the loop ("i = 0") and effectively restart it from the very beginning.
- kristopolous 9y agoWell sure, but you're just doing it wrong. Here's the goto equivalent. var itemsPassed = 0; var i, j; top: do { for (i = 0; i < items.length; i++) { for (j = 0; j < tests.length; j++) { if (!tests[j].pass(items[i])) { continue top; } } itemsPassed++; } break; } while(1); Not a great pattern but there you go.
- microcolonel 9y agoYou can just pass 0 to while, and ditch the break. ;- )
- kristopolous 9y agoNo you can't. Try it if you don't believe me
- microcolonel 9y agoOh, forgot the weird label behaviour in JS, you're right. Funny to have the label up top, but the branching condition somehow in its way.
- rtfeldman 9y agoYou can use labels to jump backwards, but not forwards. So they get you part of the way to GOTO, but not all the way!