Initially, I have some confusion regarding the prototypal structure of Javascript which might make this task challenging.
Suppose I have ...
var vector = function( x, y ) {
this.x = x || 0;
this.y = y || 0;
}
var obj = function() {
this.position = new vector( 0, 0, 0 );
}
var cube = new obj();
... how can I include a property x
to obj
that allows calling cube.x
to be the same as cube.position.x
. I believe it's possible to link properties of each other, but I am uncertain about the syntax. Something like obj.prototype.x = obj.position.x
is ineffective because obj.position
is undefined.
I desire the following functionality
alert(cube.position.x); // 0
alert(cube.x); // 0
cube.position.x = 2;
alert(cube.position.x); // 2
alert(cube.x); // 2
cube.x = 4;
alert(cube.position.x); // 4
alert(cube.x); // 4
Is this feasible?
It's worth mentioning that I am using Three.js, so modifying the objects completely is not an option; I can only add to them and their prototypes.