Last active
December 21, 2015 19:39
-
-
Save grauwoelfchen/6356070 to your computer and use it in GitHub Desktop.
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
function isArray(obj) { | |
return Object.prototype.toString.call(obj) === "[object Array]"; | |
} | |
function isFunction(obj) { | |
return Object.prototype.toString.call(obj) === "[object Function]"; | |
} | |
function forEach(list, callback) { | |
if (isArray(list)) { | |
for (var n = 0; n < list.length; n++) { | |
if (isFunction(callback)) { | |
callback.call(list[n], n); | |
} else { | |
// it fails | |
assert(isFunction(callback) === true, "[Info] This callback is not a function :("); | |
} | |
} | |
} else { | |
// it fails | |
assert(isArray(list) === true, "[Info] This list is not a array :("); | |
} | |
} | |
window.onload = function() { | |
var weapons = ["shuriken", "katana", "nunchucks"]; | |
forEach(weapons, function(index) { | |
assert(this == weapons[index], weapons[index] + " is expected value"); | |
}); | |
// not Array | |
var meal = "onigiri"; | |
forEach(meal, function(index) { | |
assert(this == meal, "this is not called"); | |
}); | |
// not Function | |
var towns = ["iga", "koga", "uraga"]; | |
forEach(towns, null); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment