Skip to content

Instantly share code, notes, and snippets.

@edgarberm
Created May 25, 2015 10:37
Show Gist options
  • Save edgarberm/1a1d30848dd95359dc3d to your computer and use it in GitHub Desktop.
Save edgarberm/1a1d30848dd95359dc3d to your computer and use it in GitHub Desktop.
forEach
var forEach = function (collection, callback, scope) {
if (Object.prototype.toString.call(collection) === '[object Object]') {
for (var prop in collection) {
if (Object.prototype.hasOwnProperty.call(collection, prop)) {
callback.call(scope, collection[prop], prop, collection);
}
}
} else {
for (var i = 0, len = collection.length; i < len; i++) {
callback.call(scope, collection[i], i, collection);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment