Javascript metaprogramming: get name of currently executing function which was dynamically rewritten

dynamic, function, javascript, metaprogramming

Solution

It is an anonymous function. You are just assigning this anonymous function to the `Element.prototype.removeChild` property, but that does not make that property a name for the function. You can assign the same function to many variables and properties, and there is no way to know the name by which it has been called.

You can however give the function a proper name which you can access as `arguments.callee.name`:

Element.prototype.removeChild = function removeChild() {
    ....
}

Problem

I am rewriting one of javascript's core methods: ``` Element.prototype._removeChild = Element.prototype.removeChild; Element.prototype.removeChild = function(){ callback(); this._removeChild.apply(this,arguments); } ``` I want to dynamically get the name of the method (in this case "removeChild") from inside of the function being dynamically rewritten. I use `arguments.callee.name` but it seems to return nothing, thinking that it is just an anonymous function. How do I get the name of the function that the anonymous function is being assigned to?

Original source