The class operator: a bridge between object and function exemplers

David Flanagan dflanagan at mozilla.com
Tue Nov 15 10:58:43 PST 2011


On 11/14/11 5:17 PM, Allen Wirfs-Brock wrote:
> On Nov 14, 2011, at 4:50 PM, David Flanagan wrote:
>
>> On 11/14/11 4:16 PM, Allen Wirfs-Brock wrote:
>>> On Nov 14, 2011, at 3:15 PM, David Flanagan wrote:
>>>> I have a bad feeling about making 'new' work with both functions and object exemplars.  If we can have two different types of classes, we're going to end up using typeof on our class objects to figure out what kind of class they are.  If I've got a value C from a library and I think it is an object exemplar, but it is in fact a constructor function, then 'class C' is going to return Function rather than C itself...
>>> Well, new'ing object exemplars has always been the central concept of our discussions about them.  Essentially it is the self style of object creation and arguably the way the prototypal instantiation is supposed to work.  It seems to be what people who really like prototypal inheritance really want to do.
>> Apparently I wasn't paying attention to the early discussions about object exemplars.  I've heard the term used, but missed the point about changing the behavior of new.  It seems to me people who want to use self-style object creation can use Object.create() and people who want to use JavaScript-style object creation can use new like we've been doing for 15 years.  (Just today I wrote a blog post explaining why I'm hoping for classes in ES.next, and included, as part of my argument, the assertion that all the proposals on the table are just syntax sugar without new language semantics.  I was wrong about that, I guess!)
> Object create doesn't do the job because it doesn't provide for calling an "initialize" methods (turns out to be a very important part of the self-style).
Its not like there's a large community of self programmers out there who 
are migrating to JavaScript...  It seems to me that if you like the 
object exemplar style, then you don't want to be using the new operator 
and constructor functions (or things that appear to be constructors 
because they're used with new).  Instead you want to define factory 
functions that use Object.create():

function Range(from, to) {
     return Object.create(Range.methods, {
         from: { value: from, enumerable: true },
         to: { value: from, enumerable: true}
     });
}
Range.methods = {
     includes: function(x) {
         return this.from <= x && x <= this.to;
     }
};
Range(1,3).includes(2);  // => true

>> Do JavaScript programmers want exemplars or do they want classes?
> They seem to be split.    Some vocally ask for better support for prototypal inheritance other vocally ask for classes.
Irakli has self-identified in this thread as wanting object exemplars 
but he has also defined a simple and useful Class() function for working 
with them.  I suspect that part of the appeal of object exemplars is 
that it is easier to write support functions like Class() for working 
with them.  That is: we can make the proponents of object exemplars 
happy with good library support.  But for classes, we need language 
support...

> I think what everybody is asking for is a better way to "created named object abstractions".  The term exemplar is just one that I introduced into the discussion to make it easier to talk about the different kinds of entities you might apply such names to.  In some language you name a "class declaration", in others you name a prototypal instance, in JS you have historically named a function. Exemplar was intended to be a generic for such "named object abstractions". An object exemplar is a self style prototype, a class exemplar is the sort of thing you find in languages llke Java.  A function exemplar is a JS constructor,
Are there existing languages that support more than one kind of exemplar 
at the language level?  It seems to me that JS is stuck with function 
exemplars, and we should work to make those better, not add complexity 
to the language by adding support for another style of exemplar.

     David



More information about the es-discuss mailing list