array2 = array1 -- by Reference or Address Only?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hal Vaughan

    array2 = array1 -- by Reference or Address Only?

    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
  • McKirahan

    #2
    Re: array2 = array1 -- by Reference or Address Only?

    "Hal Vaughan" <hal@thresholdd igital.com> wrote in message
    news:0FjDb.3577 08$Dw6.1171514@ attbi_s02...[color=blue]
    > 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[/color]
    for[color=blue]
    > 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[/color]


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


    Comment

    • Hal Vaughan

      #3
      Re: array2 = array1 -- by Reference or Address Only?

      McKirahan wrote:
      [color=blue]
      > "Hal Vaughan" <hal@thresholdd igital.com> wrote in message
      > news:0FjDb.3577 08$Dw6.1171514@ attbi_s02...[color=green]
      >> 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[/color]
      > for[color=green]
      >> 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[/color]
      >
      >
      > 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);[/color]

      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

      Comment

      • McKirahan

        #4
        Re: array2 = array1 -- by Reference or Address Only?

        "Hal Vaughan" <hal@thresholdd igital.com> wrote in message
        news:7lkDb.5542 43$Tr4.1503346@ attbi_s03...[color=blue]
        > McKirahan wrote:
        >[color=green]
        > > "Hal Vaughan" <hal@thresholdd igital.com> wrote in message
        > > news:0FjDb.3577 08$Dw6.1171514@ attbi_s02...[color=darkred]
        > >> I may have my terms mixed up (I'm not a professional programmer), but[/color][/color][/color]
        if[color=blue][color=green][color=darkred]
        > >> 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[/color][/color][/color]
        on[color=blue][color=green][color=darkred]
        > >> 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[/color]
        > > for[color=darkred]
        > >> 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[/color]
        > >
        > >
        > > 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);[/color]
        >
        > Thanks. That works if I don't want the changes to occur in both arrays,[/color]
        but[color=blue]
        > 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[/color]
        is[color=blue]
        > "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[/color]
        is[color=blue]
        > 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[/color]

        Perhaps one of these links will help:




        _ehm/?tw=reference&c ategory=forms_d ata




        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: array2 = array1 -- by Reference or Address Only?

          Hal Vaughan <hal@thresholdd igital.com> writes:
          [color=blue]
          > I may have my terms mixed up (I'm not a professional programmer), but if I
          > do this:[/color]
          [color=blue]
          > var array1 = new Array("TestOne" , "TestTwo", "TestThree" , "TestFour") ;
          > var array2 = new Array();[/color]
          .....[color=blue]
          > array2 = array1;[/color]
          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.
          [color=blue]
          > array2[0] = "SecondOne" ;
          > array1[1] = "FirstTwo";[/color]

          Here you overwrite the properties of the one array that both your
          variables point to.
          [color=blue]
          > both arrays are equal, but the string variables aren't.[/color]

          Strings are simple values. You assign different string values to
          "test1" and "test2".
          [color=blue]
          > When I assign the value of one array to another, is it only passed
          > by reference or address?[/color]

          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.
          [color=blue]
          > Is this the same in all browsers, or a glitch?[/color]

          It should be the same.

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

          Comment

          • Hal Vaughan

            #6
            Re: array2 = array1 -- by Reference or Address Only?

            Lasse Reichstein Nielsen wrote:
            [color=blue]
            > Hal Vaughan <hal@thresholdd igital.com> writes:
            >[color=green]
            >> I may have my terms mixed up (I'm not a professional programmer), but if
            >> I do this:[/color]
            >[color=green]
            >> var array1 = new Array("TestOne" , "TestTwo", "TestThree" , "TestFour") ;
            >> var array2 = new Array();[/color]
            > ....[color=green]
            >> array2 = array1;[/color]
            > 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.[/color]

            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.)
            [color=blue][color=green]
            >> array2[0] = "SecondOne" ;
            >> array1[1] = "FirstTwo";[/color]
            >
            > Here you overwrite the properties of the one array that both your
            > variables point to.
            >[color=green]
            >> both arrays are equal, but the string variables aren't.[/color]
            >
            > Strings are simple values. You assign different string values to
            > "test1" and "test2".
            >[color=green]
            >> When I assign the value of one array to another, is it only passed
            >> by reference or address?[/color]
            >
            > 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.
            >[color=green]
            >> Is this the same in all browsers, or a glitch?[/color]
            >
            > It should be the same.[/color]

            Thanks. This is a HUGE help.

            Hal
            [color=blue]
            > /L[/color]

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: array2 = array1 -- by Reference or Address Only?

              Lasse Reichstein Nielsen wrote:
              [color=blue]
              > Hal Vaughan <hal@thresholdd igital.com> writes:[color=green]
              >> When I assign the value of one array to another, is it only passed
              >> by reference or address?[/color]
              >
              > You could think of it as "passed by reference".[/color]

              You could much more simple think of it as assign a value because this
              is what you actually do.
              [color=blue]
              > More correctly would be to think of objects to be entities.[/color]

              True, but irrelevant.
              [color=blue]
              > Assigning an object to a variable makes the variable point to
              > *that* object.[/color]

              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

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: array2 = array1 -- by Reference or Address Only?

                Lasse Reichstein Nielsen wrote:
                [color=blue]
                > Hal Vaughan <hal@thresholdd igital.com> writes:[color=green]
                >> When I assign the value of one array to another, is it only passed
                >> by reference or address?[/color]
                >
                > You could think of it as "passed by reference".[/color]

                You could much more simple think of it as assign a value because this
                is what you actually do.
                [color=blue]
                > More correctly would be to think of objects to be entities.[/color]

                True.
                [color=blue]
                > Assigning an object to a variable makes the variable point to
                > *that* object.[/color]

                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

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: array2 = array1 -- by Reference or Address Only?

                  Thomas 'Ingrid' Lahn wrote:
                  [color=blue]
                  > 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?)[/color]

                  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

                  Comment

                  Working...