Created
February 6, 2013 21:26
-
-
Save ksafranski/4726005 to your computer and use it in GitHub Desktop.
Roll your own 'each' function
This file contains hidden or 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
| function each(obj,callback){ | |
| // Ensure callback is a function | |
| if (!callback || typeof(callback) !== "function") { | |
| throw new Error("Callback is not a function."); | |
| return false; | |
| } | |
| // If obj is an array | |
| if(obj.constructor === Array){ | |
| for (var i=0, z=obj.length; i<z; i++){ | |
| callback(i,obj[i]); | |
| } | |
| // If obj is an object | |
| }else{ | |
| for (var i in obj){ | |
| callback(i,obj[i]); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment