You can clone an array using Array#slice :
console.log(s); // ["bye"], ie incorrect console.log(s.slice()); // ["hi"], ie correct
A function that you can use instead of console.log that does not have this problem looks like this:
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); };
In the case of objects, unfortunately, the best method is to debug using a browser other than WebKit, or to write a complex function for cloning. If you work only with simple objects, where the order of the keys does not matter and there are no functions, you can always:
console.logSanitizedCopy = function () { var args = Array.prototype.slice.call(arguments); var sanitizedArgs = JSON.parse(JSON.stringify(args)); return console.log.apply(console, sanitizedArgs); };
All of these methods are obviously very slow, so even more so than with conventional console.log s, you should disable them after debugging is complete.
yingted Jan 27 2018-11-17T00: 00Z
source share