Skip to content

Instantly share code, notes, and snippets.

@kentbrew
Created November 23, 2011 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kentbrew/1389496 to your computer and use it in GitHub Desktop.
Save kentbrew/1389496 to your computer and use it in GitHub Desktop.
Chaining JavaScript Functions with Eval
<!doctype html>
<html>
<head><title>Chaining JavaScript Functions with Eval</title></head>
<body>
<p>I was feeling deeply frustrated with my lack of ability to figure out how to do this the Right Way, so here it is in the Wrongest Way Possible.</p>
<script>
var a = function(s) {
return s + 'a';
};
var b = function(s) {
return s + 'b';
};
var c = function(s) {
// anything that blocks could go here
var t = new Date().getTime();
for (var i = 0; i < 1000; i = i + 1) {
console.log(i);
}
var e = new Date().getTime() - t;
console.log('blocked for ' + e + ' ms');
return s + 'c';
};
var d = function(s) {
return s + 'd';
};
var k = ['a', 'b', 'c', 'd'];
// runMe starts out as whatever needs to be passed to the innermost function
var runMe = '""';
for (var i = 0, n = k.length; i < n; i = i + 1 ) {
runMe = k[i] + '(' + runMe + ')';
}
// every time you do this, Doug Crockford feels a sharp stab of pain
console.log(eval(runMe));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment