Array extras and Objects

Dmitry Soshnikov dmitry.soshnikov at gmail.com
Wed Nov 30 23:51:30 PST 2011


On 01.12.2011 11:47, Andrea Giammarchi wrote:
> 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);
>

Yep, the topic was raised before (by me in particular) of implementing 
richer standard libraries for all classes (Array and Object are first on 
this list; and especially `forEach' for Object instances). On what Allen 
said that time something like "before proposing many methods for 
classes, let's understand first where we go w/ classes at all".

Which though doesn't cancel the fact that JS has pure lib for standard 
library. Of course monkey patching allows to fix this issues ourselves 
(w/o waiting while the committee start to move in this direction), but 
of course it's good to have this functionality built-in.

Dmitry.

> Thanks for any sort of feedback.
>
> Best Regards
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

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


More information about the es-discuss mailing list