Skip to content

Instantly share code, notes, and snippets.

@nyteshade
Last active December 16, 2015 13:09
Show Gist options
  • Select an option

  • Save nyteshade/5439539 to your computer and use it in GitHub Desktop.

Select an option

Save nyteshade/5439539 to your computer and use it in GitHub Desktop.
Some functions to programmatically generate named functions in JavaScript instead of anonymous ones. Of potential interest to debugging.
function CreateNamedFnProxy(name, fn, context) {
var template = [
'(function @name() {',
' @name.fn.apply(@name.context || window, arguments);',
'})'
].join('').replace(/@name/g, name),
result = eval(template);
result.fn = fn;
result.context = context;
return result;
}
function CreateNamedFn(name, body, context) {
var template = '(function @name() {@body})'.replace(/@name/, name),
result;
if ({}.toString.call(body) === '[object Function]') {
body = /^.*?\{(.*)\}\)?(\(.*?\))?;?$/.exec(body.toString())[1];
}
body = String(body || "");
result = eval(template.replace(/@body/, body));
result.prototype = context;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment