To begin with, I don't fully understand the prototypal structure of Javascript so this may not be possible.
If 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 add a property x to obj such that calling cube.x is equivalent to cube.position.x. I think it should be possible to make properties of each reference the same value, but I'm just not sure of the syntax. Something like obj.prototype.x = obj.position.x doesn't work because obj.position is undefined.
I would like the following behaviour to be possible
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 possible?
I should probably mention that I'm working with Three.js so rewriting the objects isn't an option, just adding to them and their prototypes.