Prototypes as the new class declaration

Axel Rauschmayer axel at rauschma.de
Sat Jun 18 07:29:23 PDT 2011


> Now consider rewriting using proposed object literal extensions as:
> 
> const SkinnedMesh = THREE.Mesh <| {
>   constructor(geometry, materials) {
>     super.constructor(geometry, materials);
>  
>     this.identityMatrix = new THREE.Matrix4();
>     this.bones = [];
>     this.boneMatrices = [];
>     ...
>   }, 
>   update(camera) {
>     ...
>     super.update();
>   }
> }
> Given this declaration, you could  create a new instance by:
>   let aSM = new SkinnedMesh.constructor(aGeo, aMat);
> The object literal  form is very similar to the class declaration form except that the entity that we named and which serves as the conceptual focus of abstraction is the prototype object rather than the constructor object.

Where would the prototype of the new instance come from? Would SkinnedMesh.constructor.prototype point to SkinnedMesh?

Prototypes already have a property called “constructor”, so this part of it works out nicely.

> The correspondence is even closer if we slightly extend the new operator.  Using current new operator semantics and my alternative way of defining SkinnedMeesh and then executing:
>   let aSM = new SkinnedMesh(aGeo, aMat);
> we would get a TypeError because SkinnedMesh does not have a [[Construct]] internal method.  However, it would be a small extension to the new operator for it to invoke its operand's constructor method if the operand is an object that is not a function (alternative, we could give every object created using an object literal a [[Construct]] internal method that invokes the constructor method, its really just an alternative way to specify the same thing.)

-- 
Dr. Axel Rauschmayer

axel at rauschma.de
twitter.com/rauschma

home: rauschma.de
blog: 2ality.com





More information about the es-discuss mailing list