View Full Version : Getting array values by reference doesn't work?

03-08-2005, 04:35 PM
Look at this code:

var fruits = new Array;
fruits['pear'] = 'dont like';
fruits['apple'] = 'wait and see';
var fruitsApple = fruits['apple'];
fruitsApple = 'i love them';
alert("fruits:" + fruits['apple']);

Believe me or not, it'll print out: wait and see.

I thought all objects were manipulated by reference though...

03-08-2005, 04:54 PM
JavaScript is call by value, copy on assignment. Arrays are stored as a reference, strings are stored as raw data. fruitsApple gets assigned the string 'wait and see' by copying, and in other words is not the same object as fruits.apple. When you change the value of fruitsApple, you replace that value with a new string, 'i love them'. fruits.apple is not changed.

03-08-2005, 05:01 PM
So if I get you, there's no way to get a reference to fruits['apple'], like when manipulating objects like the following:

var form1 = documents.myForm

I guess I still don't understand, do I?

Oh by the way is fruit.apple a right syntax for fruits['apple']?

03-08-2005, 05:28 PM
No, fruits['apple'] works perfectly. It's just that with the conventions I try to keep to when talking about code (bolding for identifiers and keywords, blue colour for expressions, italics for variable parts such as onevent) it takes less work to write.

Hmm' let's try to explain this:

JavaScript is call by value. That means that any identifier is replaced with it's value when used. That value can be either null, undefined, a number primitive, a string primitive, a boolean primitive or a reference. References are used for compound data types of some sort (functions, arrays, regex, objects etc.). Values in JavaScript are immutable. They may be replaced with new values, but never actually changed. However, when using compound data types, you aren't actually working with the value, but the object that the value points to.

JavaScript is as said copy on assignment. This means that all assignments copy the value. If the value is a reference, both the original and the copy will point to the same object as all you've done is copied the reference. If the values are primitives, you instead get two different primitives that just happen to contain the same thing.

To conclude: No you can't get a reference to something of a primitive data type, and yes you do get a reference to something of a compound data type. The best you can get is a reference to the object who has the primitive as a member.

03-08-2005, 07:34 PM
Ok now I understand: assignment always copy stuff, in case stuff is an array/object, stuff is a reference so it works as I expect.

Thanks a lot for your patience :)

I have a last question concerning arrays: in case I have an associative array with text as keys, how can I parse the array to retrieve key-value pairs? eg use this array to feed a select with two options:

var fruits = new Array;
fruits['apple'] = 10;
fruits['pear'] = 30;

Once again, thanks for sharing your knowledge

03-08-2005, 08:29 PM
Using a for..in loop. E.g:
for(var elm in oFruit)

03-08-2005, 09:19 PM
this also work on arrays :thumbsup:
thankful respect to you liorean :)