The class operator: a bridge between object and function exemplers
Allen Wirfs-Brock
allen at wirfs-brock.com
Mon Nov 14 15:47:43 PST 2011
On Nov 14, 2011, at 3:09 PM, Axel Rauschmayer wrote:
>
>> 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".
>
>
> So you don’t think the two roles clash semantically? (1) “Turn an object exemplar into a class” versus (1) “get the class of an object”. Both are definitely useful! But that they can be performed by the same operator seems like a happy coincidence. If (1) was to be further refined, e.g., as Brendan suggested, by throwing an exception if an object exemplar does not have a "constructor" method then it seems both roles would drift further apart. I’m mainly arguing for a clear separation of concerns.
The two roles are the same! The value of the constructor property of an object exemplar is a function exemplar (AKA class exemplar) whose prototype property's value is the object exemplar. When you say:
let P = class {constructor() {} };
You aren't turning an object exemplar into a class exemplar your are simply accessing the class exemplar that is explicitly defined as part of the object exemplar.
The above let is also equivalent to:
let PrototypalP = {constructor(){} };
let P = class PrototypalP;
which is equivalent to:
let P = class {constructor(){} };
let PrototypalP = P.prototype;
Finally, when I talk about an object's constructor I include the possibility that 'constructor' is inherited, so the above identifies only hold when an own constructor property is explicitly provided. In fact, that is really what I mean by an object exemplar
var foo = { };
class foo is Object; //true
Object.prototype is foo; //also true
bar = new foo; //same as new foo.constructor which is the same as new Object
class bar is class foo; //and both === Object.
It all falls out of the plumbing.
Allen
>
> --
> Dr. Axel Rauschmayer
> axel at rauschma.de
>
> home: rauschma.de
> twitter: twitter.com/rauschma
> blog: 2ality.com
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111114/a7a4cc0c/attachment-0001.html>
More information about the es-discuss
mailing list