Sugar for *.prototype and for calling methods as functions

Axel Rauschmayer axel at rauschma.de
Tue Feb 21 03:52:49 PST 2012


The established way of doing this is [].forEach, "".trim, {}.valueOf. I imagine that by now, there would be no performance penalty, any more, because most engines are aware of this (ab)use. But it is indeed not very intention-revealing. It might make sense to wait with this proposal until classes are finished, but they probably won’t introduce any changes at this level.

On Feb 21, 2012, at 12:16 , Mariusz Nowak wrote:

> 
> Sorry if it already has been picked up (I searched and didn't found anything
> close that).
> 
> In my last months of work with JavaScript what that I miss a lot in ES5
> syntax is:
> 
> 1. Syntax shortcut for '.prototype'. Instead of writing
> String.prototype.trim I'd love to be able to write for example String#trim 
> (it's not proposal, just example how it may look).
> As most native ES methods are generic there are a lot of valid use cases for
> that e.g.:
> 
> Array#foEach.call(listThatsNotArray, fn);
> 
> 2. Syntax sugar for calling method as a function. In following examples I
> just place '@' at end of method that I'd like to be run as function.
> 
> Array#forEach@(listThatsNotArray, fn));
> 
> or 
> 
> trimmedListOfStrings = listOfStrings.map(String#trim@);
> 
> Last example is same as following in ES5:
> 
> trimmedListOfStrings =
> listOfStrings.map(Function.prototype.call.bind(String.prototype.trim));
> 
> This two proposals will make methods easily accessible for some functional
> constructs, and I think might be revolutionary for those who favor such
> functional way of programming.
> 
> Let me know what do you think about that.

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

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


More information about the es-discuss mailing list