Skip to content

Instantly share code, notes, and snippets.

@onestepcreative
Created March 17, 2014 22:24
Show Gist options
  • Select an option

  • Save onestepcreative/9609638 to your computer and use it in GitHub Desktop.

Select an option

Save onestepcreative/9609638 to your computer and use it in GitHub Desktop.
for each loop helper from hammer.js
var Obj = {
each: function(obj, iterator, context) {
var i, o;
// native forEach on arrays
if ('forEach' in obj) {
obj.forEach(iterator, context);
}
// arrays
else if(obj.length !== undefined) {
for(i=-1; (o=obj[++i]);) {
if (iterator.call(context, o, i, obj) === false) {
return;
}
}
}
// objects
else {
for(i in obj) {
if(obj.hasOwnProperty(i) &&
iterator.call(context, obj[i], i, obj) === false) {
return;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment