GOTO.js
web.archive.org
web.archive.org
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.
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!
Goto is a beautiful structured programming construct, the fundamental structure it represents is a skyscraper made of wet spaghetti.
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.
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.Try it if you don't believe me
So they get you part of the way to GOTO, but not all the way!
There were some earlier discussions about this: https://news.ycombinator.com/item?id=10250770 https://news.ycombinator.com/item?id=3691472
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.)?
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?`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.
In Basic, GOSUB is GOTO's younger, smarter's brother. Basically a function call.