在括号后添加新行

Adding new line after bracket

我正在代理函数 console.log 以将一些信息添加到我的日志中,我也在检查正在记录的信息是否是一个对象。我这样做是为了避免获得

类型的日志条目
2016-12-17 (22:12:51) > [object Object]

代码在传递非对象参数时工作正常。例如,命令

console.log("hello","world");

打印

2016-12-17 (22:23:53) > hello
2016-12-17 (22:23:53) > world

但是如果我也传递一个对象,代码将无法在对象后插入一个新行。例如,命令

console.log("hello",{world:true,hello:{amount:1,text:"hello"}},"world");

打印

2016-12-17 (22:27:32) > hello
2016-12-17 (22:27:32) > { world: true, hello: { amount: 1, text: hello } } 2016-12-17 (22:27:33) > world

(注意显示对象后缺少的换行符)。

代码

JQuery 3.1.1

main.js:

(function (proxied) {
    function displayArg(argument){
        var result= "";
        if(typeof argument == "object") {
            result += "{ ";
            for (i in argument) {
                result += i + ": ";
                result += (displayArg(argument[i]));
                result += ", "
            }
            result = result.substring(0,result.length - 2);
            result += " }";
            return result;
        } else {
            return argument;
        }
    }

    console.log = function () {
        var result = [];
        for (i in arguments) {
            var d = new Date();
            result[i] = d.getFullYear() + "-" + (d.getMonth() + 1)  + "-" + d.getDate() +
                " (" + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + ") > ";
            result[i] += displayArg(arguments[i]);
            result[i] += "\n";
        }
        return proxied.apply(this, result);
    }
})(console.log);

我不完全理解 objective 但关于以下过于简化的覆盖的内容呢:

var oldLog = console.log;

console.log= function(){
   var d= new Date(),
       dateString = // process string

   .....
   for(var i = 0; i<arguments.length; i++){
      oldLog(dateString, arguments[i]);          
   }

}

TL;DR change the iterator variables so they don't share name, or add a "var" to the loop definition to make sure they don't escape your desired scope.

事实证明,来自(我自己的)console.logdisplayArg 的 for 循环是 "sharing"迭代器的值 i。这是因为通过不声明迭代器变量,范围比我需要的范围更广。为了澄清,请看这个例子:

console.log({isThis:"real life"},"hello","world")

来自 console.log 的代码将在 result[0] 的开头添加一个日期,然后调用 displayArg(arguments[0]), arguments[0]{isThis:"real life"} .该函数将迭代对象属性,因此 i 将被分配值 isThis。在函数returns之后,i的值不会再回到0。相反,i 将是 isThis 因此,行

result[i] += "\n";

转换为

result[isThis] += "\n"

而不是

result[0] += "\n"

可能最明智的解决方案是在迭代器的 for 声明中添加一个 var。以下代码按预期工作:

(function (proxied) {
    function displayArg(argument){
        var result= "";
        if(typeof argument == "object") {
            result += "{ ";
            for (var i in argument) {
                result += i + ": ";
                result += (displayArg(argument[i]));
                result += ", "
            }
            result = result.substring(0,result.length - 2);
            result += " }";
            return result;
        } else {
            return argument;
        }
    }

    console.log = function () {
        var result = [];
        for (var i in arguments) {
            var d = new Date();
            result[i] = d.getFullYear() + "-" + (d.getMonth() + 1)  + "-" + d.getDate() +
                " (" + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + ") > ";
            result[i] += displayArg(arguments[i]);
            result[i] += "\n";
        }
        return proxied.apply(this, result);
    }
})(console.log);