arrows and a proposed softCall

Rick Waldron waldron.rick at gmail.com
Mon Jun 4 16:51:49 PDT 2012


On Mon, Jun 4, 2012 at 6:10 PM, Yehuda Katz <wycats at gmail.com> wrote:

> snip
>



> jQuery.each(someArray, function(i, item) {
>   // this and item are the items in the Array
>   // i is the index
> });
>
> It's too late for jQuery to fix the order now. However, in the face of
> arrows or bound functions, we probably could detect a user intent to use
> `this` for their own purposes and use that as a signal to change the order.
>
> jQuery.each = function(array, callback) {
>   for (var i=0, l=array.length; i<l; i++) {
>     item = array[i];
>     if (isBound(callback)) { callback(item, i); }
>     else { callback.call(item, i, item); }
>   }
> };
>
>

As a relevant aside - If you're not referring to the current item of the
iterating list via |this|, you could just as easily use fat arrow w/
jQuery.each() today: https://gist.github.com/2871510

Rick


I don't know whether it makes sense for a brand-new API to be designed this
> way, but for APIs that presently make (possibly misguided) use of `this` in
> place of a first parameter, the new (user-initiated, not available in ES3)
> binding behavior provides a single shot at a better way forward.
>

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120604/274846bd/attachment-0001.html>


More information about the es-discuss mailing list