Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Easy Variable Scope question...

Reply
Thread Tools

Easy Variable Scope question...

 
 
Mike P
Guest
Posts: n/a
 
      10-28-2005
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


 
Reply With Quote
 
 
 
 
web.dev
Guest
Posts: n/a
 
      10-28-2005

Mike P wrote:
> 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


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.

 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      10-28-2005
Mike P said:
>
>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;
> }


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;
}

 
Reply With Quote
 
Mike P
Guest
Posts: n/a
 
      10-28-2005
Thanks... got it

Mike


 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      10-29-2005
Mike P wrote:
> Thanks... got it
>


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
 
Reply With Quote
 
Mike P
Guest
Posts: n/a
 
      10-29-2005
Thanks RobG.... you're quickly becoming my best friend

Mike


 
Reply With Quote
 
Mike P
Guest
Posts: n/a
 
      11-08-2005
"RobG" <(E-Mail Removed)> wrote in message
news:4362c0d0$0$21679$(E-Mail Removed)...
> The fastest way to copy an array is to use its concat() method:
>
> var A = [1, 2];
> var B = A.concat();


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




 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      11-08-2005
Mike P wrote:
> "RobG" <(E-Mail Removed)> wrote in message
> news:4362c0d0$0$21679$(E-Mail Removed)...
>> The fastest way to copy an array is to use its concat() method:
>>
>> var A = [1, 2];
>> var B = A.concat();

>
> 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.


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.


> 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?


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.

>
> 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();


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
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      11-08-2005
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/>

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-08-2005
RobG wrote:

> Mike P wrote:
>> "RobG" <(E-Mail Removed)> wrote in message
>> news:4362c0d0$0$21679$(E-Mail Removed)...
>>> The fastest way to copy an array is to use its concat() method:
>>> var A = [1, 2];
>>> var B = A.concat();

>>
>> 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.

>
> 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.


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


PointedEars
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments