标签:
(function(){ console.log(arguments); //[] 是一个类数组 console.log(arguments instanceof Array);//false console.log(typeof arguments);// object var _arguments = Array.prototype.slice.call(arguments); console.log(_arguments); // [] 数组 console.log(_arguments instanceof Array);//true console.log(typeof _arguments); //object })();
var flatten = matrix.reduce(function (previous, current) { return previous.concat(current); }); console.log(flatten); //[1,2,3,4,5,6]
array.reduce(callback[, initialValue])
callback
函数接受4个参数:之前值、当前值、索引值以及数组本身。initialValue
参数可选,表示初始值。若指定,则当作最初使用的previous
值;如果缺省,则使用数组的第一个元素作为previous
初始值,同时current
往后排一位,相比有initialValue
值少一次迭代。
var sum = [1, 2, 3, 4].reduce(function (previous, current, index, array) { return previous + current; }); console.log(sum); // 10
兼容性封装(针对ie6-8)
if (typeof Array.prototype.reduce != "function") { Array.prototype.reduce = function (callback, initialValue ) { var previous = initialValue, k = 0, length = this.length; if (typeof initialValue === "undefined") { previous = this[0]; k = 1; } if (typeof callback === "function") { for (k; k < length; k++) { this.hasOwnProperty(k) && (previous = callback(previous, this[k], k, this)); } } return previous; }; }
标签:
原文地址:http://www.cnblogs.com/xiamer/p/5799109.html