By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,129 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

array2 = array1 -- by Reference or Address Only?

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.