-
-
Save jed/975059 to your computer and use it in GitHub Desktop.
scope
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
function( | |
a, // an object representing the intended scope | |
b, // a function containing unreferenced variables to be bound | |
c, // (placeholder) | |
d, // (placeholder) | |
e // (placeholder) | |
){ | |
c = []; // define a list of names, and | |
d = []; // a list of referents to which they refer. | |
for( // for each | |
e // name | |
in a // in the scope object, | |
) d[ // set, at the index | |
c.push(e) // at which the name is pushed | |
- 1 // minus 1, | |
] = a[e]; // the referent to which the name refers. | |
c.push( // at the end of the list of names, | |
"return " + b // push an expression returning the serialized function. | |
); | |
return( // return | |
e = Function // a new function (while caching the Function reference), | |
) | |
[b = "apply"] // applied using | |
(e, c) // the list of names and function body, | |
[b] // and then apply that | |
(this, d) // with the global scope and list of referents. whew! | |
} |
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
function(a,b,c,d,e){c=[];d=[];for(e in a)d[c.push(e)-1]=a[e];c.push("return "+b);return(e=Function)[b="apply"](e,c)[b](this,d)} |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Jed Schmidt <http://jed.is> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
{ | |
"name": "scope", | |
"keywords": [ "scope", "compile" ] | |
} |
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
// Define a function that refers to things that don't exist. | |
function fn(){ console.log( OH - NO - YOU - DINT|!!!!!!!!11 ) } | |
// Then call it and watch it throw. | |
try { fn(); console.log( "no problem at all!" ) } | |
catch (e) { console.log( "problem: " + e.type ) } | |
// => "problem: not_defined" | |
// No worries, crazy scope function to the rescue! | |
var scope = function(a,b,c,d,e){c=[];d=[];for(e in a)d[c.push(e)-1]=a[e];c.push("return "+b);b="apply";return(e=Function)[b](e,c)[b](this,d)} | |
// All we have to do is define the scope we're expecting, | |
var OHYESIDID = {OH:1, NO:1, YOU:1, DINT:1} | |
// an then apply it to our function. | |
fn = scope( OHYESIDID, fn ) | |
// then, we we try the same thing as before, | |
try { fn(); console.log( "no problem at all!" ) } | |
catch (e) { console.log( "problem: " + e.type ) } | |
// -1 | |
// => "no problem at all!" | |
// EVERYTHING TURNED OUT BETTER THAN EXPECTED! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment