Skip to content

Instantly share code, notes, and snippets.

@ksheedlo
Created March 4, 2014 21:04
Show Gist options
  • Save ksheedlo/9355601 to your computer and use it in GitHub Desktop.
Save ksheedlo/9355601 to your computer and use it in GitHub Desktop.
goto-in-javascript
function goto (func, line) {
var code = func.toString()
.split('\n')
.slice(line)
.join('\n');
var curlies = 0;
for (var i = 0; i < code.length; i++) {
if (code[i] === '{') {
curlies--;
}
if (code[i] === '}') {
curlies++;
}
}
code = Array(Math.max(1, curlies+1)).join('{') + code;
try {
eval(code);
} catch (e) {}
}
function yolo () {
var i = 3;
console.log(i);
i--;
// strict equality is very important
if (i !== 0) {
goto(yolo, 2);
}
}
// This is still somewhat broken. For some reason, it prints the value '66' in a loop.
yolo();
@ksheedlo
Copy link
Author

ksheedlo commented Mar 4, 2014

Ok, so when this runs, i is in goto's scope, not yolo's. let me see if I can fix that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment