473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

array2 = array1 -- by Reference or Address Only?

I may have my terms mixed up (I'm not a professional programmer), but if I
do this:
var array1 = new Array("TestOne", "TestTwo", "TestThree", "TestFour");
var array2 = new Array();
var test1 = "ScalarOne";
var test2 = test1;

array2 = array1;
array2[0] = "SecondOne";
array1[1] = "FirstTwo";
test2 = "ScalarTwo";
test1 = "NewOne";

alert("First: " + array1);
alert("Second: " + array2);
alert("One: " + test1 + ", Two: " + test2);

both arrays are equal, but the string variables aren't. When I assign the
value of one array to another, is it only passed by reference or address?
Is this the same in all browsers, or a glitch?

I have a case where I have to work with a 3d array, and if I can count on
this working, it would save work in the long run, since I wouldn't have to
update the original 3d array after parts have been copied to a 2d array for
editing (if I edit it straight in the 3d array, I have to re-write a whole
set of routines designed for 2d arrays).

Thanks for any info.

Hal
Jul 20 '05 #1
8 2883
"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:0FjDb.357708$Dw6.1171514@attbi_s02...
I may have my terms mixed up (I'm not a professional programmer), but if I
do this:
var array1 = new Array("TestOne", "TestTwo", "TestThree", "TestFour");
var array2 = new Array();
var test1 = "ScalarOne";
var test2 = test1;

array2 = array1;
array2[0] = "SecondOne";
array1[1] = "FirstTwo";
test2 = "ScalarTwo";
test1 = "NewOne";

alert("First: " + array1);
alert("Second: " + array2);
alert("One: " + test1 + ", Two: " + test2);

both arrays are equal, but the string variables aren't. When I assign the
value of one array to another, is it only passed by reference or address?
Is this the same in all browsers, or a glitch?

I have a case where I have to work with a 3d array, and if I can count on
this working, it would save work in the long run, since I wouldn't have to
update the original 3d array after parts have been copied to a 2d array for editing (if I edit it straight in the 3d array, I have to re-write a whole
set of routines designed for 2d arrays).

Thanks for any info.

Hal

You could build "array2" instead os assigning it:
for (var i=0; i<array1.length; i++) {
array2[i] = array1[i];
}

array2[0] = "SecondOne";
array1[1] = "FirstTwo";
alert("array1: " + array1 + "\n" + "array2: " + array2);
Jul 20 '05 #2
McKirahan wrote:
"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:0FjDb.357708$Dw6.1171514@attbi_s02...
I may have my terms mixed up (I'm not a professional programmer), but if
I do this:
var array1 = new Array("TestOne", "TestTwo", "TestThree", "TestFour");
var array2 = new Array();
var test1 = "ScalarOne";
var test2 = test1;

array2 = array1;
array2[0] = "SecondOne";
array1[1] = "FirstTwo";
test2 = "ScalarTwo";
test1 = "NewOne";

alert("First: " + array1);
alert("Second: " + array2);
alert("One: " + test1 + ", Two: " + test2);

both arrays are equal, but the string variables aren't. When I assign
the value of one array to another, is it only passed by reference or
address? Is this the same in all browsers, or a glitch?

I have a case where I have to work with a 3d array, and if I can count on
this working, it would save work in the long run, since I wouldn't have
to update the original 3d array after parts have been copied to a 2d
array

for
editing (if I edit it straight in the 3d array, I have to re-write a
whole set of routines designed for 2d arrays).

Thanks for any info.

Hal

You could build "array2" instead os assigning it:
for (var i=0; i<array1.length; i++) {
array2[i] = array1[i];
}

array2[0] = "SecondOne";
array1[1] = "FirstTwo";
alert("array1: " + array1 + "\n" + "array2: " + array2);


Thanks. That works if I don't want the changes to occur in both arrays, but
in this case, I want to be sure that both arrays ARE changed. I need to
know if this is standard behavior, or if it's a glitch or unreliable.

I had to do that, in one case. What would help me now is to know if this is
"standard" behavior in JavaScript. Since I'm working with a 3d array, and
all the existing routines work with a 2d routine, I thought I could do
this:

//masterArray1[x][y][z] already exists

workingArray[0] = masterArray[0][0];

Then I could use all the 2d array functions on workingArray and, if this is
normal (that changing elements in one array changes the elements in the
array I set it equal to earlier), then I wouldn't have to copy the altered
workingArray back into the original masterArray.

So is this "normal" behavior for JavaScript?

Thanks!

Hal
Jul 20 '05 #3
"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:7lkDb.554243$Tr4.1503346@attbi_s03...
McKirahan wrote:
"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:0FjDb.357708$Dw6.1171514@attbi_s02...
I may have my terms mixed up (I'm not a professional programmer), but if I do this:
var array1 = new Array("TestOne", "TestTwo", "TestThree", "TestFour");
var array2 = new Array();
var test1 = "ScalarOne";
var test2 = test1;

array2 = array1;
array2[0] = "SecondOne";
array1[1] = "FirstTwo";
test2 = "ScalarTwo";
test1 = "NewOne";

alert("First: " + array1);
alert("Second: " + array2);
alert("One: " + test1 + ", Two: " + test2);

both arrays are equal, but the string variables aren't. When I assign
the value of one array to another, is it only passed by reference or
address? Is this the same in all browsers, or a glitch?

I have a case where I have to work with a 3d array, and if I can count on this working, it would save work in the long run, since I wouldn't have
to update the original 3d array after parts have been copied to a 2d
array for
editing (if I edit it straight in the 3d array, I have to re-write a
whole set of routines designed for 2d arrays).

Thanks for any info.

Hal

You could build "array2" instead os assigning it:
for (var i=0; i<array1.length; i++) {
array2[i] = array1[i];
}

array2[0] = "SecondOne";
array1[1] = "FirstTwo";
alert("array1: " + array1 + "\n" + "array2: " + array2);


Thanks. That works if I don't want the changes to occur in both arrays,

but in this case, I want to be sure that both arrays ARE changed. I need to
know if this is standard behavior, or if it's a glitch or unreliable.

I had to do that, in one case. What would help me now is to know if this is "standard" behavior in JavaScript. Since I'm working with a 3d array, and
all the existing routines work with a 2d routine, I thought I could do
this:

//masterArray1[x][y][z] already exists

workingArray[0] = masterArray[0][0];

Then I could use all the 2d array functions on workingArray and, if this is normal (that changing elements in one array changes the elements in the
array I set it equal to earlier), then I wouldn't have to copy the altered
workingArray back into the original masterArray.

So is this "normal" behavior for JavaScript?

Thanks!

Hal


Perhaps one of these links will help:

http://www.dithered.com/javascript/array/usage.html

http://hotwired.lycos.com/webmonkey/...library/wm_ary
_ehm/?tw=reference&category=forms_data

http://www.devguru.com/Technologies/...ref/array.html
Jul 20 '05 #4
Hal Vaughan <ha*@thresholddigital.com> writes:
I may have my terms mixed up (I'm not a professional programmer), but if I
do this: var array1 = new Array("TestOne", "TestTwo", "TestThree", "TestFour");
var array2 = new Array(); ..... array2 = array1; Here you change the variable "array2" to point to the same object as
the variable "array1". Remember, arrays are objects, and objects have
identity. You never clone an object by accident.
array2[0] = "SecondOne";
array1[1] = "FirstTwo";
Here you overwrite the properties of the one array that both your
variables point to.
both arrays are equal, but the string variables aren't.
Strings are simple values. You assign different string values to
"test1" and "test2".
When I assign the value of one array to another, is it only passed
by reference or address?
You could think of it as "passed by reference". More correctly would
be to think of objects to be entities. Assigning an object to a
variable makes the variable point to *that* object. An object
reference, which might be implemented as a reference, but is subtly
different in theory.
Is this the same in all browsers, or a glitch?


It should be the same.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
Lasse Reichstein Nielsen wrote:
Hal Vaughan <ha*@thresholddigital.com> writes:
I may have my terms mixed up (I'm not a professional programmer), but if
I do this:
var array1 = new Array("TestOne", "TestTwo", "TestThree", "TestFour");
var array2 = new Array();

....
array2 = array1;

Here you change the variable "array2" to point to the same object as
the variable "array1". Remember, arrays are objects, and objects have
identity. You never clone an object by accident.


That's exactly what I needed. I didn't realize arrays were objects and
treated differently than strings. (One of the problems with being
self-taught is picking up things "half-way" or learning a concept and not
knowing just how widely it applies or in what languages it works -- I
learned Java this summer and would have expected something like this in
Java, but didn't realize it worked with arrays in JavaScript.)
array2[0] = "SecondOne";
array1[1] = "FirstTwo";


Here you overwrite the properties of the one array that both your
variables point to.
both arrays are equal, but the string variables aren't.


Strings are simple values. You assign different string values to
"test1" and "test2".
When I assign the value of one array to another, is it only passed
by reference or address?


You could think of it as "passed by reference". More correctly would
be to think of objects to be entities. Assigning an object to a
variable makes the variable point to *that* object. An object
reference, which might be implemented as a reference, but is subtly
different in theory.
Is this the same in all browsers, or a glitch?


It should be the same.


Thanks. This is a HUGE help.

Hal
/L


Jul 20 '05 #6
Lasse Reichstein Nielsen wrote:
Hal Vaughan <ha*@thresholddigital.com> writes:
When I assign the value of one array to another, is it only passed
by reference or address?
You could think of it as "passed by reference".


You could much more simple think of it as assign a value because this
is what you actually do.
More correctly would be to think of objects to be entities.
True, but irrelevant.
Assigning an object to a variable makes the variable point to
*that* object.


You never assign objects to anything. You always assign references
to an object to something, to properties in general.

Object

is a reference to a `Function' object.

Object()

calls the function it represents.

new Object()

calls the function as a constructor which creates an object that is
based upon Object (inheriting its "public" properties).

var o = new Object();

creates the object, returns a reference to it and assigns it to the
variable `o'.

(Interestingly, in my Mozilla/5.0 rv:1.7a you can also use

var o = Object();

to assign a reference to an Object object to `o'. Seems like I am
missing something, explanation anyone?)
PointedEars
Jul 20 '05 #7
Lasse Reichstein Nielsen wrote:
Hal Vaughan <ha*@thresholddigital.com> writes:
When I assign the value of one array to another, is it only passed
by reference or address?
You could think of it as "passed by reference".


You could much more simple think of it as assign a value because this
is what you actually do.
More correctly would be to think of objects to be entities.
True.
Assigning an object to a variable makes the variable point to
*that* object.


You never assign objects to anything. You always assign references
to an object to something, to properties in general.

Object

is a reference to a `Function' object.

Object()

calls the function it represents.

new Object()

calls the function as a constructor which creates an object that is
based upon Object (inheriting its "public" properties) and returns
a reference to it.

var o = new Object();

creates the object, returns a reference to it and assigns it to the
variable `o'.

(Interestingly, in my Mozilla/5.0 rv:1.7a you can also use

var o = Object();

to assign a reference to an Object object to `o'. Seems like I am
missing something, explanation anyone?)
PointedEars
Jul 20 '05 #8
Thomas 'Ingrid' Lahn wrote:
var o = new Object();

creates the object, returns a reference to it and assigns it to the
variable `o'.

(Interestingly, in my Mozilla/5.0 rv:1.7a you can also use

var o = Object();

to assign a reference to an Object object to `o'. Seems like I am
missing something, explanation anyone?)


While researching previous articles, I found this one.
For those who are interested, the answer to my question
is in ECMAScript Edition 3:

| 15.2 Object Objects
|
| 15.2.1 The Object Constructor Called as a Function
|
| When Object is called as a function rather than as a constructor,
| it performs a type conversion.
|
| 15.2.1.1 Object ( [ value ] )
|
| When the Object function is called with no arguments or with one
| argument value, the following steps are taken:
|
| 1. If value is null, undefined or not supplied, create and return
| a new Object object exactly if the object constructor had been
| called with the same arguments (section 15.2.2.1).
| 2. Return ToObject(value).
|
| 15.2.2 The Object Constructor
|
| When Object is called as part of a new expression,
| it is a constructor that may create an object.
| [...]

According to ECMAScript-3 and tested successfully in Mozilla/5.0,
this behavior applies to most of the core objects. It does not
apply, e.g., to the Math object as this cannot be called as part
of a "new" expression and to the Date object as a string will be
returned instead of an object reference.
PointedEars
Jul 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
8
by: padew | last post by:
I want make a vfariabl on the fly so: for (var i = 0; i<2; i++) { z+i=new Array("a"); fo to have: z0=a z1=a z1=a
15
by: arnuld | last post by:
-------- PROGRAMME ----------- /* Stroustrup, 5.6 Structures STATEMENT: this programmes *tries* to do do this in 3 parts: 1.) it creates a "struct", named "jd", of type "address". 2. it...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
2
by: Summercool | last post by:
I wonder in PHP5, can we use $array1 = $array2 as if it is a class? i think right now, the whole array is copied (by key and value)... and i want to assign by reference but NOT in the alias...
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
4
by: clrockwell | last post by:
Hey all, I could use some help with solving problem. Any suggestions are welcome and very appreciated. I have two arrays, $feeds and $feedDays. If they both have the same count, or $feeds...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.