Skip to content

Instantly share code, notes, and snippets.

@grauwoelfchen
Last active December 21, 2015 19:39
Show Gist options
  • Save grauwoelfchen/6356070 to your computer and use it in GitHub Desktop.
Save grauwoelfchen/6356070 to your computer and use it in GitHub Desktop.
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