Skip to content

Instantly share code, notes, and snippets.

@STRd6
Created December 5, 2013 21:09
Show Gist options
  • Save STRd6/7813942 to your computer and use it in GitHub Desktop.
Save STRd6/7813942 to your computer and use it in GitHub Desktop.
Rename function in JS
var f, rename, renamedFunction;
rename = function(fn, name) {
return Function("fn", "return (function " + name + "(){\n return fn.apply(this, arguments)\n});")(fn);
};
f = function() {};
renamedFunction = rename(f, "a_new_name");
alert(renamedFunction.name);
@CalinZBaenen
Copy link

@jonathan-annett, I thought the name field was frozen/unwritable.
Does Object.defineProperty(...) take precedence over that?

@jonathan-annett
Copy link

All i know is I've used that method with success, and seen it used elsewhere. I haven't tried it in every browser, so mileage may vary.

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