How to safely wrap `console.log`?

后端 未结 10 507
日久生厌
日久生厌 2021-01-31 14:13

Suppose I want to include some calls to console.log for some legitimate production reason, say for something like a unit test harness. Obviously I would not want th

10条回答
  •  日久生厌
    2021-01-31 15:16

    As a slight variation on Chris' answer, simply define 'log' as a property of 'console' with an empty function:

    if (typeof console === "undefined") {
        console = {
            log: function () {
                return;
            } 
       };
    }
    

提交回复
热议问题