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

前端 未结 11 1480
萌比男神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 08:02

    normally you use window[name] like

    var name ="bar"; 
    window["foo"+name] = "bam!"; 
    foobar; // "bam!"
    

    which would lead you to a function like:

    function getmc (object, name) { 
    
        window[name] = function () {}; 
        window[name].prototype = object; 
        return new window[name](); 
    
    }
    

    but then

    foo = function(){}; 
    foobar = getmc(foo, "bar"); 
    foobar; // ▶ window
    foobar.name; // foo
    x = new bar; x.name; // foo .. not even nija'ing the parameter works
    

    and since you can't eval a return statement (eval("return new name()");), I think you're stuck

提交回复
热议问题