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

前端 未结 11 1482
萌比男神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:56

    Based on the answer of @josh, this prints in a console REPL, shows in console.log and shows in the debugger tooltip:

    var fn = function() { 
       return 1917; 
    };
    fn.oldToString = fn.toString; 
    fn.toString = function() { 
       return "That fine function I wrote recently: " + this.oldToString(); 
    };
    var that = fn;
    console.log(that);
    

    Inclusion of fn.oldToString() is a magic which makes it work. If I exclude it, nothing works any more.

提交回复
热议问题