Syntactic sugar for using a function as if it were a method of its first argument

Jasper St. Pierre jstpierre at mecheye.net
Tue May 27 08:04:52 PDT 2014


It's fairly incomprehensible to me, and doesn't really have any advantages
over writing it out the long way:

    Object.getOwnPropertyDescriptor(window.HTMLFormElement.prototype,
'elements').get


window.HTMLFormElement.prototype{Object.getOwnPropertyDescriptor}('elements').get

They're both the same line length. I find the former more direct and the
latter more confusing. Namely, the whole ('elements') looks like a method
call containing one argument, rather than having a secret hidden argument
as its first.


On Tue, May 27, 2014 at 4:17 AM, Claude Pache <claude.pache at gmail.com>wrote:

>
> Often a function can be thought as if it were a method of its first
> argument. Compare:
>
>         Array.from(obj);   /* vs */   obj.toString()
>         Object.getPrototypeOf(obj);   /* vs */   obj.__proto__
>         Array.forEach(obj, func);   /* vs */   obj.forEach(func)
>         Math.clz32(num);   /* vs */   num.toFixed(2)
>
> and note the inversion of the order of the terms.
>
> So, I propose to introduce syntactic sugar to replace the terms in correct
> order, e.g.,
>
>         foo{Bar.baz}(...args)
>         // or (to be bikeshed)
>         foo.{Bar.baz}(...args)
>
> as a synonym of:
>
>         Bar.baz(foo, ...args)
>
> Here are two examples of use:
>
>
> window.HTMLFormElement.prototype{Object.getOwnPropertyDescriptor}('elements').get
>
>
> document.querySelectorAll('input[type=checkbox][name=Select_ID]:checked').{Array.from}().map(e
> => e.value).join(',')
>
> The wins are:
> * a strict left-to-right order, instead of having methods appearing
> alternatively at the right and at the left of its main operand, thus
> improving readability;
> * enabling the use of the Existential Operator for the new form, e.g.:
>
>
> window.HTMLFormElement?.prototype?{Object.getOwnPropertyDescriptor}('elements')?.get
>
> (Note that `?{` suffers from the same grammar problem as `?[` and `?(`,
> and the due fix for the two latter would also fix the former.)
>
> —Claude
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>



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


More information about the es-discuss mailing list