Skip to content

Instantly share code, notes, and snippets.

@mynyml
Forked from broofa/gist:359602
Created April 28, 2010 18:18
Show Gist options
  • Save mynyml/382487 to your computer and use it in GitHub Desktop.
Save mynyml/382487 to your computer and use it in GitHub Desktop.
function createNamedFunction() {
var _cnf = Array.prototype.slice.call(arguments, 0);
_cnf.name = _cnf.shift();
_cnf.code = _cnf.pop();
return new Function('return function NAME(ARGS) {CODE}'.
replace('NAME', _cnf.name).
replace('ARGS', _cnf.join(',')).
replace('CODE', _cnf.code))();
}
// Test it
var sys = require('sys');
var f = createNamedFunction('fred', 'x', 'y', 'return x*y');
sys.log('function defined globally? ' + (typeof(fred) != 'undefined'));
sys.log(f.toString());
sys.log(f(10,11));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment