/ Published in: JavaScript
Expand |
Embed | Plain Text
_.mixin([_.ARRAY, _.OBJECT], { each: function(collection, iterator) { var error = []; if (collection == null) error.push("_.each requires a collection parameter"); if (typeof collection !== "object") error.push("_.each expects hash or array as first argument"); if (iterator == null) error.push("_.each requires an iterator parameter (2nd parameter)"); if (error.length > 0) { throw new Error(error.join(". ")); return; } switch (iterator.length) { // the number of arguments that the iterator accepts case 0: for (var key in collection) { if (collection.hasOwnProperty(key)) { iterator.call(collection[key]); } } break; case 1: for (var key in collection) { if (collection.hasOwnProperty(key)) { iterator(collection[key]); } } break; case 2: for (var key in collection) { if (collection.hasOwnProperty(key)) { iterator(key, collection[key]); } } break; } return collection; } });
You need to login to post a comment.
