Created
April 23, 2012 22:57
-
-
Save SlexAxton/2474403 to your computer and use it in GitHub Desktop.
Safe forEach in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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