1

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.

2
  • 1
    Don't you have to return a result from the getter? (And I think there is a newer more cross browser syntax for them) Commented Nov 3, 2011 at 11:58
  • Yes, in the actual code returns do exist, but i ripped away the contents inside the functions in order to clarify the example. In regards to the syntax, most of the examples i have seen are written in this manner. Commented Nov 3, 2011 at 12:33

1 Answer 1

3

__defineGetter__ and friends are non standard.

var Camera = Object.create({}, {
  position: {
    get: function () {
      return Position.create();
    },
    set: function (p) {
      this._position = p;
    }
  }
});

Object.defineProperty(globe, "camera", {
  get: function () {
    return Object.create(Camera);
  }
});

Your going to want to use Object.defineProperty

However, why are you using getters and setters? They are evil. You should avoid them really unless your doing something clever.

Sign up to request clarification or add additional context in comments.

3 Comments

I know, but my client seems to think they're the s**t and is insisting me so much in implementing them in this project. I would never in my life use something like this in my own project (to be honest, I didnt even know this existed in JS until i was asked to do it). Anyways, Ill give that defineProperty thing a try and see how it works for me.
@user995014 tell your client, your the technical expert, not him. Seriously why do people let clients push them around. Did you mention how they infer a 99% performance loss?
Unfortunately my client is also a software development company so its hard to deal with them with this kind of stuff. They think they got the right reasons plus they put the money in this, so... as they say, the customer is always right. By the way, your approach solved my issue, thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.