Created
July 19, 2012 14:40
-
-
Save kentbrew/3144389 to your computer and use it in GitHub Desktop.
Chaining JavaScript Functions with Eval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Chaining JavaScript Functions with Eval | |
After a disastrously bad job interview I was feeling 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: | |
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment