将类数组arguments转化成数组

时间:2022-05-17 11:57:18

一、将arguments对象绑定到slice方法上

将类数组arguments转化成数组

二、 例子:

//闭包 二次封装函数 求和
function partialUsingArguments(fn) {
var args = Array.prototype.slice.call(arguments, 1);
var result = function () {
var args2 = Array.prototype.slice.call(arguments);
args = args.concat(args2);
return fn.apply(this, args); }
return result;
} var a = 1; var b = 2; var c = 3; var d = 4;
var test = function (first, second, third, forth) {
return first + second + third + forth;
};
console.log("1+2+3+4="+ partialUsingArguments(test, a, b)(c, d) );