Harmony object literals

Allen Wirfs-Brock allen at wirfs-brock.com
Mon Jun 6 07:57:05 PDT 2011

On Jun 6, 2011, at 2:31 AM, Kam Kasravi wrote:

> In the object literals proposal the following 2 examples are given below:
> var enhancedArrayProto = Array.prototype <| {
>       do (func) {return this.foreach(func)},
>       search (key {return this.indexOf(key) >= 0}
>   };
>   var myArray = enhancedArrayProto <| [0,1,2,3,4,5];
> 1. I believe search is missing a ')'
> 2. The first example is adding another prototype to Array.prototype, eg Array.prototype.prototype that contains do and search. Since this appends to the proto chain is it correct to say it's not possible to override methods using this operator? That is it's effect should only be additive?

It is creating a new object whose [[Prototype]] is Array.prototype. If this new object is used as a prototype it will provide all of the inherited Array.prototype methods plus do and search.  The new object could over-ride properties defined by Array.prototype but in this example it does not.
> 3. Since the <| operator accepts an ObjectLiteral would it be extended to include classes?

Yet to be determined. Whether it is useful/meaningful will depends upon how constructor inheritance is defined via the class declaration.

>   var enhancedArrayProto = Array.prototype <| class {
>       public do (func) {return this.foreach(func)}
>       public search (key) {return this.indexOf(key) >= 0}
>   };
> 4. I'm not quite clear on what the myArray example does, is it adding a new prototype that provides initialization of some kind?  

It is just creating a new array object whose immediate [[Prototype]] is enhancedArrayProto.  The new object has all all of the special array functionality. 

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

More information about the es-discuss mailing list