2

In other words, let the value be a variable. Something like this:

var a=3,b=4;
var obj = {x : a, y : b};
alert(obj.x); //will display '3'
a=2;
alert(obj.x); //want it to display '2' - how can i accomplish this?
1
  • No C++ style references in JS, use methods as others have suggested Commented May 8, 2013 at 15:29

4 Answers 4

5

Make it a method:

var a = 3,
    b = 4,
    obj = {
        x: function () {
            return a;
        },
        y: b
    };
a = 2;

And call it like:

obj.x()  // returns 2

DEMO: http://jsfiddle.net/67NwY/2/

Otherwise there's no native (and supported in older browsers) way to get the "live" value of a by using obj.x. Another answer here provides the use of Object.defineProperty that can do this.

You can apply this to obj.y as well, if you wish to do the same.

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

11 Comments

If you want to be able to update it with just a reference to obj, you can have the function take a value, like so?: jsfiddle.net/67NwY/1
@ColinDeClue That's true, but the OP seems to always want the value of obj.x to reference the value of a
Correct, and the one I updated still will. It'll just also update the value of a.
@ColinDeClue Very true. I just don't see a point when you can use a = 2; like the OP originally had. I just feel like it's unnecessary in this scenario. The OP is probably leaving out code/examples, so it could all definitely be restructured to do something like what you have, but not deal with "global" variables
If you only had reference to obj, but not a at some point.
|
5

Here's one approach:

var a=3,b=4;
var obj = {x : function(){return a;}, y : function(){return b;}};
alert(obj.x()); // displays '3'
a=2;
alert(obj.x()); // displays '2'

Comments

3

Make it an object

var a = {
  val:3
};

var obj = {x : a};

alert(obj.x.val); //will display '3'
a.val=2;
alert(obj.x.val); //will display '2'

http://jsfiddle.net/tzfDa/2/

Property getters and setters Adding to what Ian said, in newer versions of JS, you can use property getters and setters. Ian used the programmatic way of defining them, there's also a literal syntax (since your question title mentions "object literal"). Note that by adding a setter also, we allow setting of the value from obj.x and a;

var a = 3;
var o = {
    get x() {
        return a;
    },
    set x(value) {
        a = value;
    }
};    
alert ( o.x ); // 3
a = 2;
alert( o.x ); // 2
o.x = 4;
alert(a); // 4

2 Comments

Did you test in a jsFiddle? jsfiddle.net/tzfDa You're setting the x property as a reference to the object a. So the alerts show "[object Object]". And if you change it to var obj = {x: a.val}; it still doesn't work because it's still referencing a primitive - jsfiddle.net/tzfDa/1
Ahh now I see what you're getting at!
1

You can do this using property descriptors (as of ECMAScript version 5).

var a = 3;
var obj = {};
Object.defineProperty(obj, "x", { get : function(){ return a; } });

alert(obj.x); // 3

a = 2;

alert(obj.x); // 2

http://jsfiddle.net/b7prK/

10 Comments

It's the same as methods, but with syntactic sugar
@JuanMendes It's not the same as methods. You don't have to call obj.x. It's still a property. I'd say that's very different.
It's syntactic sugar, it is implemented as functions without parameters as you can tell by the fact that you pass in a function and use its closure.
@JuanMendes It doesn't matter how it's defined...the difference is how you access it. To get its value, you use obj.x, not obj.x(); if you do alert(typeof obj.x), you get "number", not "function". Simply using methods doesn't achieve this, which may or may not break something
Nice, the other solutions are OK too but using this solution will maintain a simple alert to 'obj.x' instead of 'obj.x()' or 'obj.x.val'. Thanks a lot guys!
|

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.