Skip to content

Instantly share code, notes, and snippets.

@mantismamita
Created May 22, 2015 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mantismamita/1b092e5427a1d8e66abc to your computer and use it in GitHub Desktop.
Save mantismamita/1b092e5427a1d8e66abc to your computer and use it in GitHub Desktop.
myForEach
var fruits= ['apple', 'orange', 'lemon', 'fig', 'banana'];
var numbers= [1, 2, 3, 4, 5];
var person = {firstName:"John", lastName:"Doe", age:46};
var mixed= [];
mixed[0]= person;
mixed[1]= new Date();
mixed[2]= fruits;
Array.prototype.myForEach = function(callback, thisObj) {
var len= this.length;
for(var i=0; i< len; i++) {
callback(i, this[i], this);
}
};
function printIndex(index, element, array) {
console.log( "[" + index + "] is " + element );
}
function addS(index, element, array) {
console.log( element + "s" );
}
fruits.myForEach(printIndex);
fruits.myForEach(addS);
numbers.myForEach(printIndex);
numbers.myForEach(addS);
mixed.myForEach(printIndex);
mixed.myForEach(addS);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment