Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

pointers in JS

Posted on 2003-11-28
6
331 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

792 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