Skip to content

Instantly share code, notes, and snippets.

@dherman
Created July 27, 2012 21:39
Show Gist options
  • Save dherman/3190598 to your computer and use it in GitHub Desktop.
Save dherman/3190598 to your computer and use it in GitHub Desktop.
Analog to Function constructor for building closures
(function() {
var hasOwnProperty = Object.prototype.hasOwnProperty;
var Function = hasOwnProperty.constructor;
function isIdentifier(s) {
return /[a-zA-Z_$][a-zA-Z_$0-9]*/.test(s);
}
Closure = function Closure(args, body, env) {
var closedVars = [], closedVals = [];
for (var key in env) {
if (!hasOwnProperty.call(env, key))
continue;
if (!isIdentifier(key))
throw new Error("invalid variable name: " + key);
closedVars.push(key);
closedVals.push(env[key]);
}
// validate the syntax of the body as a FunctionBody
(new Function(body));
// validate the syntax of the formal parameters as FormalParameters
Function.apply(null, args);
// declare closed variables initialized from an array bound to `this`
var closeVars = "var " + closedVars.map(function(x, i) {
return x + " = this[" + i + "]";
}).join(",") + ";";
// return the function that closes over the variables
var returnClosure = "return function(" + args.join(",") + "){" + body + "};";
closedVals.maker = new Function(closeVars + returnClosure);
return closedVals.maker();
};
Closure.prototype = Function.prototype;
})();
var f = new Closure([], "return x + y", { x: 1, y: 77 });
console.log(f()); // 78
var g = new Closure(["z"], "return x + y + z", { x: 1, y: 77 });
console.log(g(13)); // 91
@fitzgen
Copy link

fitzgen commented Jul 28, 2012

Disclaimer: trying to start discussion, not argument; not insinuating that I invented the following technique, just reimplemented it.

What advantages does the technique you present here have over using something like https://github.com/fitzgen/wu.js/blob/master/lib/wu.js#L631 which doesn't use new Function or eval? Just different goals?

Usage:

function add3(x, y, z) {
  return x + y + z;
}

var f = wu.partial(add3, 1, wu.___, 77);

console.log(f(13));
// 91

Copy link

ghost commented Jul 28, 2012

var global = new Function('return this')(),
    globals = Object.getOwnPropertyNames(global).filter(isIdentifier);

var sandbox = new Closure([], loadedScriptSource, makeIdentityPreservingMembrane(global));
sandbox();

@kg
Copy link

kg commented Jul 28, 2012

fitzgen: Your solution adds the overhead of another function call and reuses the original function body which means you only get one JITted version of the function for all closed over value sets. The eval method (and new Function method) both create new function bodies for each set of variables, which means that if the types are different you'll get better JITted performance. It does mean it'll use more memory, though...

@dherman
Copy link
Author

dherman commented Jul 28, 2012

@fitzgen: They're different operations. Yours is much more statically well-behaved; mine is much more reflective. I don't advocate using reflection except when you know for sure you need it — for example, when writing a JIT such as Shumway. Otherwise I'd advocate using a currying API like yours.

Dave

@fitzgen
Copy link

fitzgen commented Jul 28, 2012

Cool, thanks for the replies, guys.

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