class sugar

Allen Wirfs-Brock allen at wirfs-brock.com
Sat Jun 4 12:38:20 PDT 2011


On Jun 3, 2011, at 9:43 PM, Peter Michaux wrote:

> ...
> closure-based system ----------------------------------
> 
> function makePoint(x, y) {
>    return {
>        getDistance: function() {
>            return Math.sqrt(x * x + y * y);
>        }
>    };
> }
> 
> 
> The classes strawman is proposing terse syntax to make the
> new/prototype/this-based system more aesthetic.
> 
> http://wiki.ecmascript.org/doku.php?id=harmony:classes
> 
> 
> Introducing class syntax will be one of the most monumental changes
> ever made to ECMAScript and I suggest that unfortunately the proposal
> is targeting the wrong version of creating constructors. I've found
> the closure-based system more compelling than the
> new/prototype/this-based system. My memory even makes me think that
> this list was heading in the direction of sugaring the closure-based
> system in 2008.
> 
> http://www.mail-archive.com/es-discuss@mozilla.org/msg00335.html
> 
> Was using class syntax to sugar the closure-based system thrown out as
> a possibility for some reason?


Note that the enhanced object literal proposals that were also promoted http://wiki.ecmascript.org/doku.php?id=harmony:object_literals  significantly enhance what can be expressed by an object literal used in the closure pattern:

const pointProto = { /* shared methods that don't use closure captured state */}

function makePoint(x, y) {
   return pointProto <| {
       getDistance() {   //getDistance is a non-enumerable property, like methods should be
           return Math.sqrt(x * x + y * y);
       }
   };
}



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


More information about the es-discuss mailing list