原文:http://rubyqiu.php.blog.163.com/blog/static/8614152320098211576782/
通常将类数组(Array-Like)arguments转换成数组可以采用以下的方法:
- Array.apply(null,arguments);
- Array.prototype.slice.call(arguments,0);
- Array.prototype.splice.call(arguments,0,arguments.length);
<script>
function f(){
var as1 = Array.prototype.slice.call(arguments,0);
var as2 = Array.apply(this, arguments);
var as3 = Array.apply(null, arguments);
var as4 = Array.prototype.splice.call(arguments,0,arguments.length);
console.log(as1);
console.log(as2);
console.log(as3);
console.log(as4);
}
f(1, 2, 3);
f("a", "b");
f(true);
</script>