Create a gist now

Instantly share code, notes, and snippets.

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
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