Naming an anonymous function

后端 未结 6 1959
心在旅途
心在旅途 2020-12-05 18:33

Is it possible to somehow set a name for anonymous functions?

There is no need to add function names to the namespace for anonymous functions but I would like to avo

6条回答
  •  隐瞒了意图╮
    2020-12-05 19:12

    If dynamic function name is the issue. You can try this:

    function renameFunction(name, fn) {
        return (new Function("return function (call) { return function " + name +
           " () { return call(this, arguments) }; };")())(Function.apply.bind(fn));
    } 
    
    renameFunction('dynamicName',function() { debugger })();
    

    source: Nate Ferrero

提交回复
热议问题