Array extras and Objects

Andrea Giammarchi andrea.giammarchi at gmail.com
Wed Nov 30 23:47:09 PST 2011


Let's say this is an attempt to bring some new, easy to implement, method
for the native Object.prototype
Specially about forEach, the most used Array.prototype method out there,
it's quite clear JS developers would like to have similar method to iterate
over objects, as key:value pairs rather than index:value.

Almost every framework/library out there has an "error prone" each(obj,
callback) method, able to automagically recognize if the passed obj is an
array, an arrayLike, or an object.
A classic failing scenario is a function used as object, where the length
property won't mean it's an arrayLike entity.

As summary, I wonder if this proposal could make things more explicit, easy
to remember, and faster, if implemented natively:
https://gist.github.com/1410420

where more details about the "why" and "how", through examples, are
explained in my post:
http://webreflection.blogspot.com/2011/11/array-extras-and-objects.html

The quick version, and all I am asking is an opinion about this, is

obj.forEach(function (value, key, original) {
  // all true conditions
  original.hasOwnProperty(key);
  value === original[key];
  original === obj;
  this === optionalContext;
}, optionalContext);

Thanks for any sort of feedback.

Best Regards
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111201/b09d04cb/attachment-0001.html>


More information about the es-discuss mailing list