Exemplars & static properties.

Gavin Barraclough barraclough at apple.com
Thu Nov 24 13:23:23 PST 2011


On Nov 14, 2011, at 12:16 PM, Allen Wirfs-Brock wrote:

> Using object exemplars, the named abstraction is itself an instance of the abstraction. Some people seem to be perfect happy to only have and use this form of object abstraction.  However, others prefer to think in a more "classical" style where the named object representing an abstraction is a distinct kind of object that is not itself an instance of the abstraction. In practice, these two views of object abstraction are very closely related/  For example, a very simple addition to the above definition essentially turns Point into a "function exemplar".  Function exemplars can also be thought of as "class exemplars":
> 
> let Point = {
>      x: 0,
>      y: 0,
>      constructor(x, y) {
>          this.x = x;
>          this.y = y;
>      }
> }.constructor;   //<----------- note added property reference

Apologies in advance if this has already been proposed, but I was looking at the exemplar examples, and wondering if an additional small extension to object literal syntax might provide a rather nice way to support 'static' (or class) properties, per the ClassPropertyDefinition properties of the minimal classes proposal.  Could we permit dot notation to allow properties to be defined on other properties already being defined within the literal?  E.g.:

let o1 = {
    o2: {
        x: 1
    }
    o2.y: 2
};

This example would define an object o1 with one property, o2, which in turn contains two properties, x & y.  I think this dot notation could be generally useful in defining object literals, but it seems like it could be particularly useful when defining exemplar objects since it would allow properties to be defined on the constructor property, for example defining a function property on the constructor:

let Point = {
     x: 0,
     y: 0,
     constructor(x, y) {
         this.x = x;
         this.y = y;
     },
     constructor.fromPolar: function(radius, azimuth) {
         return new Point(radius * Math.cos(azimuth), radius * Math.sin(azimuth));
     }
}.constructor;

let p = Point.fromPolar(Math.sqrt(2), Math.PI/4)
print(p.x, p.y);

If this dot notation could also be used when defining methods (functions omitting the function keyword, as per the constructor method in the exemplars example), and if these methods were to also have their prototype property set to point to the containing object literal (again, as per the constructor method), then it would also make it possible for methods to be defined with in the intention of use with the 'new' keyword as alternative constructors for the type, e.g.:

let Point = {
     x: 0,
     y: 0,
     constructor(x, y) {
         this.x = x;
         this.y = y;
     },
     constructor.Polar(radius, azimuth) {
         this.x = radius * Math.cos(azimuth);
         this.y = radius * Math.sin(azimuth);
     }
}.constructor;

let p = new Point.Polar(Math.sqrt(2), Math.PI/4)
print(p.x, p.y);

Further, if dot notation were also allowed in defining getter/setter properties I believe it would make this syntax fully sufficient to define all types of properties on the constructor as described in the ClassPropertyDefinition grammar in the minimal classes proposal.

Any thoughts on this suggestion would be appreciated.

Many thanks & happy thanksgiving!,
Gavin.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111124/7e025dec/attachment.html>


More information about the es-discuss mailing list