Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Created April 3, 2012 19:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save WebReflection/2294934 to your computer and use it in GitHub Desktop.
Save WebReflection/2294934 to your computer and use it in GitHub Desktop.
all you need to `each(obj, cb [,context])`
var each = function (Array) {"use strict";
/*! all you need to `each(obj, cb [,context])`
* @author WebReflection
* @license WTFPL - http://en.wikipedia.org/wiki/WTFPL
* @gist https://gist.github.com/2294934
*/
var
toString = {}.toString,
hasOwnProperty = {}.hasOwnProperty,
array = toString.call([]),
NL = typeof NodeList != "undefined" ? NodeList : Array,
isArrayLike = function isArrayLike(obj) {
return obj != null &&
(
obj instanceof Array ||
obj instanceof NL
) ||
toString.call(obj) == array ||
(
(
(t = typeof obj) != "function" &&
t != "string"
) &&
typeof(l = obj.length) == "number" &&
l === ~~l
)
;
},
forEach = [].forEach || function forEach(callback, context) {
for (var
obj = this, i = 0, length = obj.length;
i < length;
++i
)
i in obj && callback.call(context, obj[i], i, obj)
;
},
t, l
;
return function each(obj, callback, context) {
if (isArrayLike(obj))
forEach.call(obj, callback, context)
;
else for (var key in obj)
hasOwnProperty.call(obj, key) && callback.call(context, obj[key], key, obj)
;
};
}(Array);
@brettz9
Copy link

brettz9 commented Aug 28, 2013

You mentioned at http://webreflection.blogspot.com/2012_04_01_archive.html that this was a proposal... I'd like very much to see something like this implemented as a standard--where did you propose this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment