Connecting Tech Pros Worldwide Forums | Help | Site Map

Easy Variable Scope question...

Mike P
Guest
 
Posts: n/a
#1: Oct 28 '05
I have, what should be, a simple scope problem. Can you help me fix this?

I'm trying to end up like this:
originalArray = [1,2,7] and newArray = [6,7,12].

Instead I wind up like this:
originalArray = [6,7,12] and newArray = [6,7,12].

Here's my code:

var originalArray = [1,2,7]; /* I'm pulling this from a form and want
to keep it as is, but the function modifies it */
var newArray = createNewArray( originalArray ); /* I want to wind up
with newArray = [6,7,12] */

function createNewArray( x ) {
for ( var i = 0; i < x.length; i++ ) { x[i] += 5 }
return x;
}

I've also tried this:

function createNewArray( x ) {
var y = x;
for ( var i = 0; i < y.length; i++ ) { y[i] += 5 }
return y;
}

and this:

var originalArray = [1,2,3];
var tmpArray = originalArray;
var newArray = createNewArray( tmpArray );

function createNewArray( x ) {
for ( var i = 0; i < x.length; i++ ) { x[i] += 5 } return x;
}

all with the same disasterous results.

Thanks!

Mike



web.dev
Guest
 
Posts: n/a
#2: Oct 28 '05

re: Easy Variable Scope question...



Mike P wrote:[color=blue]
> I have, what should be, a simple scope problem. Can you help me fix this?
>
> I'm trying to end up like this:
> originalArray = [1,2,7] and newArray = [6,7,12].
>
> Instead I wind up like this:
> originalArray = [6,7,12] and newArray = [6,7,12].
>
> Here's my code:
>
> var originalArray = [1,2,7]; /* I'm pulling this from a form and want
> to keep it as is, but the function modifies it */
> var newArray = createNewArray( originalArray ); /* I want to wind up
> with newArray = [6,7,12] */
>
> function createNewArray( x ) {
> for ( var i = 0; i < x.length; i++ ) { x[i] += 5 }
> return x;
> }
>
> I've also tried this:
>
> function createNewArray( x ) {
> var y = x;
> for ( var i = 0; i < y.length; i++ ) { y[i] += 5 }
> return y;
> }
>
> and this:
>
> var originalArray = [1,2,3];
> var tmpArray = originalArray;
> var newArray = createNewArray( tmpArray );
>
> function createNewArray( x ) {
> for ( var i = 0; i < x.length; i++ ) { x[i] += 5 } return x;
> }
>
> all with the same disasterous results.
>
> Thanks!
>
> Mike[/color]

Hi Mike,

You were very close to the solution, taking one of your solutions from
above and modified it as follows will work:

function createNewArray(x)
{
var y = new Array();

for (var i = 0; i < x.length; i++)
{
y[i] = x[i] + 5
}

return y;
}

var originalArray = [1, 2, 7];
var newArray = createNewArray(originalArray);

You were missing the following:

var y = new Array();

It was because you were referencing the same Array that was causing the
problem. If you explicitly created a new array, that will solve your
problem.

Lee
Guest
 
Posts: n/a
#3: Oct 28 '05

re: Easy Variable Scope question...


Mike P said:[color=blue]
>
>I have, what should be, a simple scope problem. Can you help me fix this?
>
>I'm trying to end up like this:
> originalArray = [1,2,7] and newArray = [6,7,12].
>
>Instead I wind up like this:
> originalArray = [6,7,12] and newArray = [6,7,12].
>
>Here's my code:
>
> var originalArray = [1,2,7]; /* I'm pulling this from a form and want
>to keep it as is, but the function modifies it */
> var newArray = createNewArray( originalArray ); /* I want to wind up
>with newArray = [6,7,12] */
>
> function createNewArray( x ) {
> for ( var i = 0; i < x.length; i++ ) { x[i] += 5 }
> return x;
> }[/color]

No matter how you assign an array variable to another variable,
you are only assigning a reference to the original array.
You need to create an entirely new array:

function createNewArray(x) {
var y=new Array(x.length);
for(var i=0;i<x.length;i++) {
y[i]=x[i]+5;
}
return y;
}

Mike P
Guest
 
Posts: n/a
#4: Oct 28 '05

re: Easy Variable Scope question...


Thanks... got it :)

Mike


RobG
Guest
 
Posts: n/a
#5: Oct 29 '05

re: Easy Variable Scope question...


Mike P wrote:[color=blue]
> Thanks... got it :)
>[/color]

The fastest way to copy an array is to use its concat() method:

var A = [1, 2];
var B = A.concat();

Done.

concat() was introduced with JavaScript 1.2, so maybe some 'version 4'
browsers won't have it, but any even remotely modern browser will.

The usual caveat applies - elements of A that are objects (arrays or
whatever) are 'copied' as references. The other solutions offered do
the same.

--
Rob
Mike P
Guest
 
Posts: n/a
#6: Oct 29 '05

re: Easy Variable Scope question...


Thanks RobG.... you're quickly becoming my best friend :)

Mike


Mike P
Guest
 
Posts: n/a
#7: Nov 8 '05

re: Easy Variable Scope question...


"RobG" <rgqld@iinet.net.au> wrote in message
news:4362c0d0$0$21679$5a62ac22@per-qv1-newsreader-01.iinet.net.au...[color=blue]
> The fastest way to copy an array is to use its concat() method:
>
> var A = [1, 2];
> var B = A.concat();[/color]

I got this... tried it with a simple array, and it worked. Now, I've
restructured things a bit by mixing arrays and objects. And, it's broken.
Is there something that I need to do to make sure this complex mix of
objects and arrays is copied, not referenced with an object?

Here's my code:

var A = {name: "name", contents: [{a:1, b:1},{a:6, b:3}]};
var B = {name: "newname"};
B.contents = A.contents.concat();
B.contents[0].a = 99;
document.write("This should be 1, but it equals " + A.contents[0].a);

Thanks again, for the assist!

Mike




RobG
Guest
 
Posts: n/a
#8: Nov 8 '05

re: Easy Variable Scope question...


Mike P wrote:[color=blue]
> "RobG" <rgqld@iinet.net.au> wrote in message
> news:4362c0d0$0$21679$5a62ac22@per-qv1-newsreader-01.iinet.net.au...[color=green]
>> The fastest way to copy an array is to use its concat() method:
>>
>> var A = [1, 2];
>> var B = A.concat();[/color]
>
> I got this... tried it with a simple array, and it worked. Now, I've
> restructured things a bit by mixing arrays and objects. And, it's broken.[/color]

Only types that are passed by value (strings, numbers) will be copied,
anything passed by reference (e.g. objects, arrays, functions) will have
a referenced passed.

[color=blue]
> Is there something that I need to do to make sure this complex mix of
> objects and arrays is copied, not referenced with an object?[/color]

Yes, look at what the thing is then do the right thing. The more
complex you make the stuff you put in there, the more complex the
operation becomes.

At some point you realise it is easier to create your own custom objects
(probably using a constructor) and give them the methods they need to
pass values.
[color=blue]
>
> Here's my code:
>
> var A = {name: "name", contents: [{a:1, b:1},{a:6, b:3}]};
> var B = {name: "newname"};
> B.contents = A.contents.concat();[/color]

For A, you may want to give it a getContents() method so you can do
something like:

B.contents = A.getContents();

or similar. You need to learn a bit about designing classes (although
JavaScript doesn't have classes, the concept of constructor functions
for objects is very similar) and interfaces to know how to do this properly.

Anyhow, a quick 'n dirty copy function (sorry, you'll have to tidy this
up yourself...):

function copyThing(x)
{
if ( 'number' == typeof x) return x;
if ( 'string' == typeof x) return x;
if ( 'object' == typeof x){
if (x.constructor && Array == x.constructor) {
var z = [];
for (var i=0, len=x.length; i<len; ++i){
z[i] = copyThing(x[i]);
}
return z;
}
if (x.constructor && Object == x.constructor) {
var z = {};
for (prop in x){
z[prop] = copyThing(x[prop]);
}
return z;
}
}
return 'Fell through: ' + x.constructor;
}

// Call using:
B.contents = copyThing(A.contents);

// Just to prove we did copy B:
alert(
'A.contents[1][\'a\']: ' + A.contents[1]['a']
+ '\n' +
'B.contents[1][\'a\']: ' + B.contents[1]['a']
);

B.contents[1]['a'] *= 3;

alert(
'A.contents[1][\'a\']: ' + A.contents[1]['a']
+ '\n' +
'B.contents[1][\'a\']: ' + B.contents[1]['a']
);



Note that it doesn't deal with functions, booleans, etc. so if you want
to stuff those into objects then copy them you'll need to add more if
statements. You can write a similar function that gets the contents of
the objects and prints them out so you can check the properties/contents.




--
Rob
VK
Guest
 
Posts: n/a
#9: Nov 8 '05

re: Easy Variable Scope question...


Besides RobG answer in the above posting:

If you are interested in a versatile data serialization media you may
also check JSON way:
<http://www.crockford.com/JSON/>

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#10: Nov 8 '05

re: Easy Variable Scope question...


RobG wrote:
[color=blue]
> Mike P wrote:[color=green]
>> "RobG" <rgqld@iinet.net.au> wrote in message
>> news:4362c0d0$0$21679$5a62ac22@per-qv1-newsreader-01.iinet.net.au...[color=darkred]
>>> The fastest way to copy an array is to use its concat() method:
>>> var A = [1, 2];
>>> var B = A.concat();[/color]
>>
>> I got this... tried it with a simple array, and it worked. Now, I've
>> restructured things a bit by mixing arrays and objects. And, it's
>> broken.[/color]
>
> Only types that are passed by value (strings, numbers) will be copied,
> anything passed by reference (e.g. objects, arrays, functions) will have
> a referenced passed.[/color]

There is no such thing as "pass by reference" in JS/ECMAScript,
object references are values.


PointedEars
Mike P
Guest
 
Posts: n/a
#11: Nov 8 '05

re: Easy Variable Scope question...


"Thomas 'PointedEars' Lahn" <PointedEars@web.de> wrote in message
news:1398237.UCfbMImogU@PointedEars.de...
[color=blue]
> There is no such thing as "pass by reference" in JS/ECMAScript,
> object references are values.
>
> PointedEars[/color]

PE,

Thank God you're here! Without your valued assistance, I would be learning
and accomplishing wonderful work, yet simultaneously falling victim to these
tragic faux pas. Keep up the good work.

:)

Mike


Mike P
Guest
 
Posts: n/a
#12: Nov 8 '05

re: Easy Variable Scope question...


"RobG" <rgqld@iinet.net.au> wrote in message
news:jmXbf.1370$uQ6.67840@news.optus.net.au...
[color=blue]
> At some point you realise it is easier to create your own custom objects
> (probably using a constructor) and give them the methods they need to pass
> values.[/color]

Very good point. It's clearly an area I need to study a bit more... so I'm
doing just that.
[color=blue]
> For A, you may want to give it a getContents() method so you can do
> something like:
>
> B.contents = A.getContents();[/color]

Thanks... and thanks for the code sample. I get the sense that my code will
be a lot cleaner, easier to work with, and easier to follow if I take
advantage of custom objects and their methods.

Mike


John G Harris
Guest
 
Posts: n/a
#13: Nov 8 '05

re: Easy Variable Scope question...


In article <1398237.UCfbMImogU@PointedEars.de>, Thomas 'PointedEars'
Lahn <PointedEars@web.de> writes[color=blue]
>RobG wrote:
>[/color]
<snip>[color=blue][color=green]
>> Only types that are passed by value (strings, numbers) will be copied,
>> anything passed by reference (e.g. objects, arrays, functions) will have
>> a referenced passed.[/color]
>
>There is no such thing as "pass by reference" in JS/ECMAScript,
>object references are values.[/color]

Object references are values, yes, and are what's passed, yes, but the
things referenced are passed by reference.

What's confusing P.Ears is that the programmer has no choice in
javascript about how anything is passed. As a result, "passed by value"
and "passed by reference" are phrases that don't have much use when
talking about javascript. However, if you're comparing javascript with
other languages or with general computer principles then they are useful
(and there is such a thing).

John
--
John Harris
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#14: Nov 8 '05

re: Easy Variable Scope question...


John G Harris <john@nospam.demon.co.uk> writes:
[color=blue]
> Object references are values, yes, and are what's passed, yes, but the
> things referenced are passed by reference.[/color]

The confusement here comes from "pass by reference" having an existing
meaning that is different from how you are using it. Passing by
reference means passing an l-value, so it makes no sense to say that
an object is being passed by reference.
[color=blue]
> What's confusing P.Ears is that the programmer has no choice in
> javascript about how anything is passed. As a result, "passed by value"
> and "passed by reference" are phrases that don't have much use when
> talking about javascript.[/color]

They do, in describing how Javascript passes its arguments of
functions: by value, *not* by reference.

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Closed Thread