为什么arguments不能直接转换成数组?
有没有更简单的方法让arguments像数组一样转换成字符串?
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]
这不仅仅是在控制台中查看。想法是将字符串用于ajax请求,然后另一端解析它,并希望得到一个数组,但实际上却得到了其他结果。
[].slice.call
代替Array.prototype.slice.call
。 - Moritz Roesslerarguments.toJSON = [].slice; console.log(JSON.stringify(arguments));
:-) - BergitoJSON
函数分配给一个对象,很好知道,谢谢 =) - Moritz Roessler