Javascript Array each()

Description

Javascript Array each()



// Array.each - Array.forEach alias.
Array.prototype.each = Array.prototype.forEach;

// Object.eachKey - Iterates over key/value pairs.
Object.prototype.eachKey = function (cb) {
  var k = Object.keys(this);
  for (var i = 0; i < k.length; i++)
    cb(k[i], this[k[i]]);/*from  w ww  .j  av  a 2s . c  o  m*/
};

// times - Runs cb() n times.
var times = function (n, cb) {
  for (var i = 0; i < n; i++) cb(n);
};

// Array.range - for a 2-item array, creates an array of numbers in that range.
Array.prototype.range = function () {
  var ar = [];
  for (var i = this[0]; i <= this[1]; i++)
    ar.push(i);
  return ar;
};

Javascript Array each()

/*/*ww w .  ja  va2s  . c o  m*/
 * When module will be defined, module enumerable shall replace method each.
 */

Array.prototype.each = function(){
 for (var i = 0; i < this.length; i++) 
  Array.prototype.each.yield(this[i]) 
}



PreviousNext

Related