How to dynamically set a function/object name in Javascript as it is displayed in Chrome

前端 未结 11 1481
萌比男神i
萌比男神i 2020-11-28 07:21

This is something which has been bugging me with the Google Chrome debugger and I was wondering if there was a way to solve it.

I\'m working on a large Javascript ap

11条回答
  •  佛祖请我去吃肉
    2020-11-28 07:54

    If you want to dynamically create a named function. You can use new Function to create your named function.

    function getMutableCopy(fnName,proto) {
        var f = new Function(`function ${fnName}(){}; return ${fnName}`)()
        f.prototype = proto;
        return new f();
    }
    
    getMutableCopy("bar",{}) 
    // ▶ bar{}
    

提交回复
热议问题