Prototypes as the new class declaration

Axel Rauschmayer axel at rauschma.de
Sat Jun 18 08:53:29 PDT 2011


>> Where would the prototype of the new instance come from? Would SkinnedMesh.constructor.prototype point to SkinnedMesh?
> 
> Of course -- Allen's proposal just "rotates" the (constructor, prototype) pair so the named or "outer" object is the prototype.
> 
> This matches the syntax. It's a bit odd that class syntax has a body that contains constructor, yet the class name binds to that inner constructor function. It works, or we can make it work -- it just seems unusual given the otherwise-pellucid rules for naming objects expressed by braced bodies (initialisers, functions).


Right, I agree that the appeal of binding the prototype to the class name (instead of the constructor) is that everything makes more sense, especially instanceof and subclassing.

I still don’t understand the “rotates” part, though. The following looks to me like a normal object literal (with the prototype set via the prototype operator). Nothing special about it.

> const SkinnedMesh = THREE.Mesh <| {
>  constructor(geometry, materials) {
>    super.constructor(geometry, materials);
> 
>    this.identityMatrix = new THREE.Matrix4();
>    this.bones = [];
>    this.boneMatrices = [];
>    ...
>  }, 
>  update(camera) {
>    ...
>    super.update();
>  }
> }


Then I would have to make the following assignment:
> SkinnedMesh.constructor.prototype = SkinnedMesh;

Now the following works (without a separate protocol):
> var sm = new SkinnedMesh.constructor(...);


A next step would be the syntactic sugar suggested by Allen:
new SkinnedMesh(...) => new SkinnedMesh.constructor(...)

However: THREE.Mesh would have to refer to the prototype (similarly to how things are done above), for this to work.

-- 
Dr. Axel Rauschmayer

axel at rauschma.de
twitter.com/rauschma

home: rauschma.de
blog: 2ality.com





More information about the es-discuss mailing list