pointers in JS

Is there a way for two variables to point to the same object?
LVL 4
noam_dzAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fritz_the_blankCommented:
I don't see why not:

var objForm = document.forms[0];
var objForm1 = document.forms[0];


FtB
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NetGrooveCommented:
Yes, there is a way.

Here an example:

<script>

var myObj = new Object();
myObj.size = 19;
var oRef = myObj;
alert(myObj.size);
alert(oRef.size);
oRef.size=20;
alert(oRef.size);
alert(myObj.size);

</script>

0
VincentPugliaCommented:
Hi,

  I'm not sure I understand the question -- are you asking about pointers?  or variables?

function people(fName, lName)
{
  this.fName = fName;
  this.lName = lName;
  this.name = lName;

  this.setName = function ()
  {  
    this.name = arguments[0];
    return this.name
  }
  this.setBoth_nope = function()
  {
    this.lName = this.name;
    this.name = arguments[0];
    return this.lName + ' = ' + this.name;
  }
}

a = new people('john', 'doe')
alert(a.lName)
alert(a.name)
alert(a.setName('deer'))
alert(a.name)
alert(a.lName)
alert(a.setBoth_nope('buck'))}

Vinny
0
ZontarCommented:
Objects are manipulated by value; primtive data types are manipulated by reference. Since strings are immutable, the question has no bearing on them.

Here, oneYear is a reference to bday, and any changes in it are also made to bday:

var bday = new Date(2003, 03, 14);
alert(bday.toLocateString());  //  displays: Monday, April 14, 2003 00:00:00
var oneYear = bday;
oneYear.setYear(oneYear.getFullYear() + 1);
alert(bday.toLocateString());  //  displays: Wednesday, April 14, 2004 00:00:00

Here, twoYears and bday are separate objects, and changes made to one have no effect on the other:

var twoYears = new Date(bday.getFullYear(), bday.getMonth(), bday.getDate());
alert(twoYears.toLocaleString());  //  displays: Wednesday, April 14, 2004 00:00:00
twoYears.setYear(twoYears.getFullYear() + 1);
bday.setYear(bday.getFullYear() - 1);
alert(twoYears.toLocaleString());  //  displays: Thursday, April 14, 2005 00:00:00
alert(bday.toLocateString());  //  displays: Monday, April 13, 2003 00:00:00

x and y are primitive values (numbers):

var x = 2;
var y = x;
y++;
alert(y);  //  displays: 3
alert(x);  //  displays: 2

Notice this depends on the type; you can't get around it by using the Number constructor:

var i = new Number(10);
alert(i);  //  displays: 10
var j = i;
j++;
alert(j);  //  displays: 11
alert(i);  //  displays: 10 (not 11)

But we can use the Object constructor to link the properties of two variables:

var x = new Object();
x.value = 5;
alert(x.value);  //  displays: 5
y = x;
y.value++;
alert(y.value);  //  displays: 6
alert(x.value);  //  displays: 6 (not 5)

This also applies to custom objects using constructor functions:

function Thing(it)
{
  this.it = it;
}

var thing1 = new Thing("you");
var thing2 = thing1;
alert(thing2.it);  //  displays: you
thing2.it = thing2.it.replace("you", "me");
alert(thing1.it);  //  displays: me
0
jaysolomonCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: fritz_the_blank {http:#9838182} & NetGroove {http:#9838232}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.