Why doesn't arguments stringify to an array ?
Is there a less verbose way to make arguments stringify like an array?
function wtf(){
console.log(JSON.stringify(arguments));
// the ugly workaround
console.log(JSON.stringify(Array.prototype.slice.call(arguments)));
}
wtf(1,2,3,4);
-->
{"0":1,"1":2,"2":3,"3":4}
[1,2,3,4]
wtf.apply(null, [1,2,3,4]);
-->
{"0":1,"1":2,"2":3,"3":4}
[1,2,3,4]
This isn't just to watch in the console. The idea is that the string gets used in an ajax request, and then the other side parses it, and wants an array, but gets something else instead.
[].slice.call
instead ofArray.prototype.slice.call
– Louisarguments.toJSON = [].slice; console.log(JSON.stringify(arguments));
:-) – LeeannaleeannetoJSON
function to an Object, good to know, thanks =) – Louis