Skip to content

Instantly share code, notes, and snippets.

@SlexAxton
Created April 23, 2012 22:57
Show Gist options
  • Save SlexAxton/2474403 to your computer and use it in GitHub Desktop.
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