Created
April 3, 2012 19:28
-
-
Save WebReflection/2294934 to your computer and use it in GitHub Desktop.
all you need to `each(obj, cb [,context])`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?