pointers in JS

Is there a way for two variables to point to the same object?
LVL 4
noam_dzAsked:
Who is Participating?
 
fritz_the_blankConnect With a Mentor Commented:
I don't see why not:

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


FtB
0
 
NetGrooveConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.