4

I want to give variables reference in javascript.

For example, I want to do:

a=1
b=a
a=2

and have b=2, and change accordingly to a.

Is this possible in javascript? If it isn't is there a way to do like a.onchange = function () {b=a}?

What I wanted to do was make a function like makeobject which make an object and puts it in an array and than returns it like

function makeobject() {
   objects[objects.length] = {blah:'whatever',foo:8};
   }

so than I could do

a=makeobject()
b=makeobject()
c=makeobject()

and later in the code do

for (i in objects) {
   objects[i].blah = 'whatev';
}

and also change the values of a,b and c

3 Answers 3

5

You can pull this off using the new object literal syntax which works well in most browsers.

var o = {
    _a : null,
    get a() { return this._a; },
    set a(v) { this._a = v; },
    get b() { return this._a; },
    set b(v) { this._a = v; }
};

o.a = 2;
o.b = 3;
o.a = 4;
alert(o.b); // alerts 4

Another alternative is to create your own reference object.

function Ref(obj, name, otherName) {
    this.obj = obj;
    this.name = name;
    this.otherName = otherName;
}

Ref.prototype.assign = function (v) {
    this.obj[this.name] = this.obj[this.otherName] = v;
}

var o = {
    a : 1,
    b : 4
};

var ref = new Ref(o, 'a', 'b');
ref.assign(3);

alert(o.b);
Sign up to request clarification or add additional context in comments.

Comments

2

It's kind of possible. You'd have to use an object however:

var a = new Object();
a.val = 6;

alert(a.val);

var b = a;
b.val = 5;

alert(a.val);

Edit: I was playing around with another alternative (even though an answer was already marked), and here's another solution:

function makeobj()
{
    if(typeof makeobj.baseobj === 'undefined')
    {
        makeobj.baseobj = { 'blah': 'whatever', 'foo': 8 };
    }
    return makeobj.baseobj;
}

var a = makeobj();
var b = makeobj();
var c = makeobj();

alert(a['blah']);
alert(b['blah']);
alert(c['blah']);

b['blah'] = 'i changed the value';

alert(a['blah']);
alert(b['blah']);
alert(c['blah']);

The alternative solution will allow you to create n objects and change the values whenever you want and have the changes propagate to other variables created by the makeobj() function.

8 Comments

This is not true. EVERYTHING IS PASSED AND ASSIGNED BY VALUE IN JS. Sorry for shouting :-) Please note that in the above "passed by value" statement there is no mention of a "copy/clone/duplicate". An object is what it is and a variable is just a transient name for an object. However, +1 for showing how an object can be mutated to get a similar effect to what the post was asking.
I'll have to look into that a little more in the ECMA spec (if I can find a good source). +1 for getting me interested in reading dry white papers ;)
Little nitpick, typeof makeobj.baseobj === 'undefined' is the better route to take when checking for undefined pre-ECMAScript5.
Simply out of curiosity.. Why? (change the answer just 'cuz I like to believe in people ;))
@Demian - Wow I actually meant to type out the reason... Anyway the reason is that before version 5 the undefined variable was mutable so any code could change its value.
|
0

This is not possible.

What are you trying to do?
We can probably find you more Javascript-y ways to do it.

3 Comments

Is there a particular reason it's not possible? Could you clarify a bit as to why this is?
@templatetypedef Because an assignment in JavaScript is always a value copy to a "slot" (variable or property)? (In JS object values always represents themselves. Even thinking Java terms, "references to objects" are themselves just values).
The reason is so I could make variables and change all the values later at the same time. I added it to the question

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.