Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 543
user730569 Avatar asked Jun 15 '12 21:06

user730569


1 Answers

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() {
    ....
}
like image 58
lanzz Avatar answered Nov 17 '22 22:11

lanzz