次を使用して配列を複製できますArray#slice
。
console.log(s); // ["bye"], i.e. incorrect
console.log(s.slice()); // ["hi"], i.e. correct
代わりに使用できるconsole.log
この問題のない関数は次のとおりです。
console.logShallowCopy = function () {
function slicedIfArray(arg) {
return Array.isArray(arg) ? arg.slice() : arg;
}
var argsSnapshot = Array.prototype.map.call(arguments, slicedIfArray);
return console.log.apply(console, argsSnapshot);
};
残念ながら、オブジェクトの場合、WebKit 以外のブラウザーで最初にデバッグするか、複雑な関数を記述して複製するのが最善の方法のようです。キーの順序が重要ではなく、関数がない単純なオブジェクトのみを操作している場合は、いつでも次のことができます。
console.logSanitizedCopy = function () {
var args = Array.prototype.slice.call(arguments);
var sanitizedArgs = JSON.parse(JSON.stringify(args));
return console.log.apply(console, sanitizedArgs);
};
これらのメソッドはすべて明らかに非常に遅いため、通常console.log
の s よりもさらに遅いので、デバッグが完了したらそれらを取り除かなければなりません。