The class operator: a bridge between object and function exemplers

Allen Wirfs-Brock allen at wirfs-brock.com
Mon Nov 14 14:24:41 PST 2011


On Nov 14, 2011, at 1:15 PM, Axel Rauschmayer wrote:

>> The class operator  can prefix any UnaryExpression, not just object literals.  That means that the class operator can be used to classify objects:
>> 
>> if (class p === Point) ...
>> 
>> Note that many object-oriented abstraction designers consider this for of class testing to be an anti-pattern. 
>> 
>> It also works with instances of the standard ES built-in constructors:
>> 
>> if (class new Array(1,2,3,4) === Array) ... //this will be true
>> 
>> so are these:
>> 
>> if (class [1,2,3,4] === Array) ...
>> if (class [].pop === Function) ...
> 
> Is it a good idea to overload this operator?
> 
> 1. class objectExemplar: “turn the `objectExemplar` into a class”
> 2. class obj: “get the class of obj”
> 
> I would give operator #2 the name `classof` and let `class` only return the own property value of `constructor`.


I'm not over-loading the class operator. I the above I was simply pointing out that this is what the operator I defined does in those circumstances. The concept of a constructor function being the "class" of an object is one of those things that has been around JS apparently from the beginning. It just didn't have a suggestively named operator.  However an object is instantiated, this operator is simply reporting that the "class"of the object  whatever is accessible as the object's "constructor".

Allen





More information about the es-discuss mailing list