for loop wrapper

by Sam Fereday

JavaScript

// ...
function forEach(arr, cb, ctx) {
  let len = arr.length,
    i = len - 1;
  ctx = ctx ? ctx : this;
  for (i; i >= 0; i--) {
    cb.call(ctx, arr[i], i, len);
  }
}

// A nice little aray
let arr = [1, 2, 3, 4, 5, 6];

// You could extend native but it's a bit nasty to do.
forEach(arr, function(item, i, len) {
  console.log(item, i, len);
}, this);