5 ms·
GOTO.js
- saas_co_de 9y agowould also recommend https://github.com/contra/blink-polyfill https://github.com/contra/blink-polyfill
- pygy_ 9y agoIncidentally, I just released https://github.com/pygy/gosub https://github.com/pygy/gosub for the cases where you want to use pkg.scripts and either npm or yarn as a task runner, but don't want to force your users to depend on one of these tools at the expense of the other. `gosub` will run subtasks with the binary you've used to launch the main task. What does it have over npm-run-all? It passes parameters to subtasks. That's all. It doesn't take any options and can't run tasks in parallel unlike npm-run-all.
- zacharynewton 9y agoGreat advice: "If you are seriously considering using this script, please don't."
- md224 9y agoFunny script, though I can't help but point out a flaw: it doesn't play well with unbracketed conditionals. For example: if (num > 5) goto foo; will not behave as expected, because "goto foo;" is replaced by two separate statements, the second of which is outside the scope of the conditional and will always be called. Maybe they could fix this by tying the statements together with a comma instead of separating with semicolons? (EDIT: I don't think a comma will work, but adding brackets around the two statements should do the trick. JS doesn't seem to mind redundant blocks.) Still funny though.
- amirbehzad 9y agoGoto in JavaScript?! it's like Ebola++
- microcolonel 9y agoI wrote one of these in 2014 after my boss called goto a mistake. Goto is a beautiful structured programming construct, the fundamental structure it represents is a skyscraper made of wet spaghetti.
- kmill 9y agoI really wish JavaScript had goto statements, because it would make it less of a hassle to make code generators that target it. Before the new block scoping constructs (which I assume would make implementing goto more complicated), was there anything preventing JavaScript from having gotos, other than an authoritarian "we know better than you"? At least JavaScript has labeled blocks that you can break out of!
- crispytx 9y agoBASIC is dope. Maybe hipsters will start using it to build WebApps in a few years, and it will dethrone JavaScript as the hipster language de jour.
- tmalsburg2 9y agoJavaScript can hardly be called the language de jour when has been pretty much the only serious frontend language in the (short) history of the web.
- asadlionpk 9y agoNo matter how inefficient, JS has made webdev available to order of magnitude more people than it was before.
- 9y ago
- kristopolous 9y agoGoto 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
- 9y ago
- albertzeyer 9y agoRelated for Python: https://stackoverflow.com/questions/6959360/goto-in-python https://stackoverflow.com/questions/6959360/goto-in-python There were some earlier discussions about this: https://news.ycombinator.com/item?id=10250770 https://news.ycombinator.com/item?id=10250770 https://news.ycombinator.com/item?id=3691472 https://news.ycombinator.com/item?id=3691472
- amelius 9y agoDoes it still work?
- amatera 9y agoThere is nothing special about this code. I guess it should still work (but please don't use it ;-) )
- z3t4 9y agoYou can do a lot of interesting stuff by parsing the code before running it. For example implementing ES6+ features in runtime's that only support ES5. The possibilities are limitless! It can be really fun when the parsed code itself gets parsed, like a dream within a dream.
- princekolt 9y agoI guess this is also what made Lisp and Smalltalk so powerful, right?
- Tarean 9y agocallCC is my favorite goto weirdness. import Control.Monad.Cont import Data.Function (fix) main = for 0 (<3) (+1) $ \i -> do liftIO (print i) for z p s body = do (jmp, i) <- label z when (p i) $ do body i jmp (s i) label start = callCC (\cont -> return (fix $ \jmp cur -> cont (jmp, cur), start)) Guess you could do this in javascript via trampolining?
- dahjelle 9y agoWhile I fully agree that GOTO can lead to unmaintainable spaghetti (and is rather more likely to do so than other constructs), I wonder if it is actually more understandable than function calls for someone approaching programming for the very first time? I know I cut my teeth using GOTO before discovering GOSUB in BASIC. Does anyone know of any research on whether some programming constructs are more easily approachable for beginnings and can serve as a tool to understand further concepts (scope, functions, etc.)?