I want to achieve a syntax similar to this one using JS setters and getters:
globe.camera.position = Position.create();
This is supossed to be equivalent to this expression:
globe.getCamera().setPosition(Position.create());
I have no problem in creating the “first level” of getters/setters, the .camera part, like this:
function Camera() {
var x,y,z;
this.__defineGetter__("camera", function() {
alert("This is the camera getter");
});
this.__defineSetter__("camera", function(position) {
alert("This is the camera setter");
});
}
...
globe=new Camera();
globe.camera=...
c=globe.camera;
...
But im not quite sure on how to define the position getter inside camera. I am trying something like this but it wont work:
function Position() {
this.__defineGetter__("position", function() {
alert("This is the position getter");
});
}
globe.camera=new Position();
pos=globe.camera.position;
The alert inside the getter wont show up. Any clue on this? Is it even possible to achieve this behaviour? I have searched quite a lot on Google but havent been able to hit the right search terms, and the examples for getters/setters tend to be very simple. Thanks in advance.
__defineGetter__and friends are non standard.Your going to want to use
Object.definePropertyHowever, why are you using getters and setters? They are evil. You should avoid them really unless your doing something clever.