array2 = array1 -- by Reference or Address Only?

H

Hal Vaughan

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
 
M

McKirahan

Hal Vaughan said:
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 = array1;
}

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

Hal Vaughan

McKirahan said:
Hal Vaughan said:
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 = array1;
}

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
 
M

McKirahan

Hal Vaughan said:
McKirahan said:
Hal Vaughan said:
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 = array1;
}

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/reference/javascript_code_library/wm_ary
_ehm/?tw=reference&category=forms_data

http://www.devguru.com/Technologies/ecmascript/quickref/array.html
 
L

Lasse Reichstein Nielsen

Hal Vaughan said:
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
 
H

Hal Vaughan

Lasse said:
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
 
T

Thomas 'PointedEars' Lahn

Lasse said:
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
 
T

Thomas 'PointedEars' Lahn

Lasse said:
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
 
T

Thomas 'PointedEars' Lahn

Thomas said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top