Create a gist now

Instantly share code, notes, and snippets.

@mantismamita /script.js Secret
Created May 22, 2015

What would you like to do?
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