Is there a method I can override on a JavaScript object to control what is displayed by console.log?

后端 未结 4 1031
旧时难觅i
旧时难觅i 2020-12-07 01:29

I\'m thinking in particular of Chrome, though Firebug would be interesting to. I\'ve tried toString() and valueOf(), but neither of those seem to be used. Interestingly, i

4条回答
  •  情歌与酒
    2020-12-07 01:36

    There's no way I know of. Your best bet will be to define a toString() method on the object you want to log and then call it, either directly or indirectly:

    var o = {};
    o.toString = function() {
        return "Three blind mice";
    };
    
    console.log("" + o);
    console.log(o.toString());
    

提交回复
热议问题