Solved

pointers in JS

Posted on 2003-11-28
6
327 Views
Last Modified: 2012-06-21
Is there a way for two variables to point to the same object?
0
Comment
Question by:noam_dz
6 Comments
 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 25 total points
ID: 9838182
I don't see why not:

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


FtB
0
 
LVL 10

Assisted Solution

by:NetGroove
NetGroove earned 25 total points
ID: 9838232
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
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9839597
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
 
LVL 11

Expert Comment

by:Zontar
ID: 9841325
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
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10384038
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now