Javascript Array myInject(blk, accum)

Description

Javascript Array myInject(blk, accum)



Array.prototype.myInject = function(blk, accum){
  if (accum === null){
    var accum = this.shift();
  }/*w  w w . j av a 2s  .  c om*/
  this.myEach(function(elem){
     accum = blk(accum, elem);
  });
  return accum;
};

var result = [1,2,3,4,5].myInject(function(acc, el){
    return acc * el;
}, 0);

console.log(result);



PreviousNext

Related