Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2005
    Location
    France, Bordeaux
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Getting array values by reference doesn't work?

    Look at this code:

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

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    Last edited by liorean; 03-08-2005 at 03:57 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New Coder
    Join Date
    Mar 2005
    Location
    France, Bordeaux
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So if I get you, there's no way to get a reference to fruits['apple'], like when manipulating objects like the following:
    Code:
    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']?
    Last edited by MyttO; 03-08-2005 at 04:03 PM.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    New Coder
    Join Date
    Mar 2005
    Location
    France, Bordeaux
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    var fruits = new Array;
    fruits['apple'] = 10;
    fruits['pear'] = 30;
    Once again, thanks for sharing your knowledge
    Last edited by MyttO; 03-08-2005 at 06:36 PM.

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Using a for..in loop. E.g:
    Code:
    var
        oFruit={
            apple:10,
            pear:30},
        oPairs=[],
        elm;
    for(var elm in oFruit)
        oPairs.push([elm,oFruit[elm]]);
    alert(oPairs.join(';'));
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    New Coder
    Join Date
    Mar 2005
    Location
    France, Bordeaux
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    this also work on arrays
    thankful respect to you liorean


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •