Chrome JavaScript developer console: Is it possible to call console.log() without a newline?

前端 未结 12 850
生来不讨喜
生来不讨喜 2020-11-29 05:07

I\'d like to use console.log() to log messages without appending a new line after each call to console.log(). Is this possible?

12条回答
  •  無奈伤痛
    2020-11-29 05:29

    Something about @shennan idea:

    function init(poolSize) {
          var pool = [];
          console._log = console.log;
          console.log = function log() {
            pool.push(arguments);
            while (pool.length > poolSize) pool.shift();
        
            draw();
          }
          console.toLast = function toLast() {
            while (pool.length > poolSize) pool.shift();
            var last = pool.pop() || [];
            for (var a = 0; a < arguments.length; a++) {
                last[last.length++] = arguments[a];
            }
            pool.push(last);
        
            draw();
          }
          function draw() {
            console.clear();
            for(var i = 0; i < pool.length; i++)
              console._log.apply(console, pool[i]);
          }
        }
        
        function restore() {
          console.log = console._log;
          delete console._log;
          delete console.toLast;
        }
        
        init(3);
        console.log(1);
        console.log(2);
        console.log(3);
        console.log(4);    // 1 will disappeared here
        console.toLast(5); // 5 will go to row with 4
        restore();

提交回复
热议问题