Tr.js inheritance pattern

I want to know why the three.js code is structured as follows:

THREE.Camera = function(){
    THREE.Object3D.call(this);
    //add more Camera specific properties and methods
}

THREE.Camera.prototype = new THREE.Object3D();
THREE.Camera.prototype.constructor = THREE.Camera;

THREE.Camera.prototype.//add more camera specific methods...

I want to know why they call the base constructor in the current constructor, as well as for the prototype?

In MDN, they show a pattern like this:

subType = function(){
    //new properties for subType
}

subType.prototype = new baseType();

They don't have a call to the base constructor in the subType constructor, so why does THREE.js do this?

+5
source share
1 answer

Because each instance THREE.Object3Dinherits from THREE.Object3D.prototype, setting in this way THREE.Camera.prototype, each instance THREE.Cameraalso inherits from THREE.Object3D.prototype.

If you do not, instances THREE.Camerado not inherit any properties assigned THREE.Object3D.prototype.

So both parts are important:

  • THREE.Camera.prototype , THREE.Object3D.prototype, .

  • - , .


, , , . , THREE.Object3D , ?

, THREE.Object3D.prototype, , :

THREE.Camera.prototype = Object.create(THREE.Object3D.prototype);
THREE.Camera.prototype.constructor = THREE.Camera;

: Object.create

+7

All Articles