everycode
everycode copied to clipboard
2015年1月23日 D7 希望每日一题能继续,来发简单的。arguments类型转换。
把arguments转化为array类型。 case:
function arguments2array(){
// todo code body
}
arguments2array(1); //=> [1]
arguments2array(1, 4, 6); //=> [1, 4, 6]
arguments2array(1, {}); //=> [1, {}]
Array.prototype.slice.call(arguments);
//arguments.slice();
汗
[].slice.call(arguments);
function argments2array(){
return [].slice.call(arguments);
}
每日一水
火钳刘明
function argments2array(){
Array.prototype.slice.call(arguments,0);
}
return arguments
function argments2array(){
return Array.prototype.slice.call(arguments,0);
}
function argments2array(){ return Array.prototype.slice.call(arguments); }
function test(){
var args = Array.prototype.slice.apply(arguments);
alert(args);
}
function argments2array (){
try{
return Array.prototype.slice.call(arguments);
} catch(e){
var arr = [];
for(var i = 0,len = arguments.length; i < len; i++){
arr[i] = arguments[i];
}
}
return arr;
}
return Array.prototype.slice.call(arguments);
function arguments2array(){ return [].slice.call(arguments,0) }
function argments2array(){ return arguments.valueOf(); }
@HelloAspNet 好思路
玩点小花样吧,ES5来帮忙
function argments2array(){
return [].map.call(arguments,function(item){
return item
});
}
或者ES6(不过V8都还不支持from方法)
function argments2array(){
return [].from(arguments)
}