Javascript Array myInject(callback)
Array.prototype.myInject = function(callback) { var sum = this[0]; for(var i = 1; i < this.length; i++) { sum = callback(sum, this[i]);//from w w w. ja va 2 s . co m } return sum; }; var callback = function(sum, value) { return sum += value; };
Array.prototype.myInject = function(callback) { let acc = this[0]; const remArr = this.slice(1, this.length); remArr.myEach((el) => {//from w w w. jav a 2s .c o m acc = callback(acc, el); }); return acc; }; console.log([1,2,3,4].myInject((a, b) => { return a + b; }));
Array.prototype.myInject = function (callback) { var result = this[0]; this.slice(1).myEach(function (el) { result = callback(result, el);/* w w w.j a v a 2 s.co m*/ }); return result; } var c = a.myInject(function (accum, el) { return accum + el; }); console.log(c);