简体   繁体   English

Javascript元编程:获取当前执行的函数的名称,该函数已被动态重写

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

I am rewriting one of javascript's core methods: 我正在重写javascript的核心方法之一:

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. 我想从正在动态重写的函数内部动态获取方法的名称(在本例中为“ removeChild”)。 I use arguments.callee.name but it seems to return nothing, thinking that it is just an anonymous function. 我使用arguments.callee.name但似乎什么也不返回,以为它只是一个匿名函数。 How do I get the name of the function that the anonymous function is being assigned to? 如何获得将匿名函数分配给的函数的名称?

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. 您只是将此匿名函数分配给Element.prototype.removeChild属性,但这不会使该属性成为函数的名称。 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 : 但是,您可以为函数指定一个适当的名称,您可以将其作为arguments.callee.name进行访问:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM