PROBLEM: When inheriting an object's properties and methods, a child object seems to loose connection with parent's 'this'. To illustrate it better, look at my example:
function RigidBody() {
this.local = new Matrix4x3();
this.position = new vec3();
...
}
RigidBody.prototype = {
...
setPosition: function(vec) {
this.position.copy(vec);
this.local.setTranslation(this.position);
}
...
};
CameraObject.prototype = new RigidBody();
CameraObject.prototype.constructor = CameraObject;
function CameraObject() {
this.fov = ...
}
CameraObject.prototype.add = ...;
var camera = new CameraObject();
camera.add(...); // Works fine;
camera.setTranslation(...); // Throws "TypeError: Cannot read property 'setTranslation' of undefined
// And on another PC it throws: "http://localhost:8080/js/rigidBody.js(61): Uncaught exception: TypeError: Cannot convert 'this.local' to object"
How to bypass it? I found a workaround for this problem by assigning this.this = this;
to parental object and replacing every this by this.this. Unfortunately as a result I need to add .this to every camera function call like this: camera.this.setPosition(...);