Skip to content

Instantly share code, notes, and snippets.

@SlexAxton
Created April 23, 2012 22:57
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save SlexAxton/2474403 to your computer and use it in GitHub Desktop.
Safe forEach in JavaScript
Array.prototype.safeForEach = function ( fn ) {
var len = this.length;
for ( var cur = 0, cur < len; ++cur ) {
for ( var i = 0; i < len; ++i ) {
if ( this.hasOwnProperty[ i ] ) {
if ( i === cur ) {
fn && fn( this[ i ], i, this );
}
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment