Passing and copying arrays by value. How-to?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nachoexr
    New Member
    • Sep 2008
    • 2

    Passing and copying arrays by value. How-to?

    I have this function
    Code:
    function test(n)
    {
       alert(n[0]);
       var a = n;
       a.shift();
       alert(n[0]);
    }
    then I run this:
    Code:
    test(Array(1,2));
    The first alert says:
    Code:
    1,2
    but the second says:
    Code:
    2
    How can I pass by value, or copy an array by value? I can't understand memory management in javascript.
  • rnd me
    Recognized Expert Contributor
    • Jun 2007
    • 427

    #2
    array are always passed by ref.

    one way around it:

    replace:

    var a = n;

    with

    var a = [].concat(n);

    EDIT:

    your test function uses the same array.
    shift both returns the bottom element and removes it from the array.

    thus, index 1 becomes index 0 after the shift, and you see 2 instead of 1 and 2 because 1 is gone.

    Comment

    • nachoexr
      New Member
      • Sep 2008
      • 2

      #3
      Oh, I see. Thank you for the tip.

      With you answer I've created this simple function:
      Code:
      function copyArray(a,b)
      {
         var n=a.length;
         for(var i=0;i<n;i++)
         {
            b[i]=a[i];
         }
      }
      As the only way to "copy" an array, don't you?

      EDIT:
      I've found this better function:


      I hope it helps.

      Comment

      • rnd me
        Recognized Expert Contributor
        • Jun 2007
        • 427

        #4
        must faster to do:
        Code:
         function copyArray(a,b) {
         b= [].concat(a);
        }

        Comment

        • FLEB
          New Member
          • Aug 2008
          • 30

          #5
          Yeah, it's a bit of an old thread, but just in case anyone else bumps into it... a smarter way to implement this would be as an Array method, not a simple function:

          Code:
          Array.prototype.copy = function () {
             return [].concat(this);
          }
          Then, it becomes a method of every Array. Easy to read, simple to remember:

          Code:
          var myArray = [1,2,3,4,5];
          var myArrayCopy = myArray.copy();
          Now, I've got to go change my own Array.copy() helper-method. I'd been using a for/in loop. I don't know why I didn't think of concat() or similar.

          Comment

          • rnd me
            Recognized Expert Contributor
            • Jun 2007
            • 427

            #6
            Originally posted by FLEB
            Yeah, it's a bit of an old thread, but just in case anyone else bumps into it... a smarter way to implement this would be as an Array method, not a simple function:

            Code:
            Array.prototype.copy = function () {
               return [].concat(this);
            }
            Then, it becomes a method of every Array. Easy to read, simple to remember:

            Code:
            var myArray = [1,2,3,4,5];
            var myArrayCopy = myArray.copy();
            Now, I've got to go change my own Array.copy() helper-method. I'd been using a for/in loop. I don't know why I didn't think of concat() or similar.
            i think that if we are to pollute a global namespace like Array, we should have a very good reason for doing so, more than just renaming concat copy...

            you don't need an extra copy method, or a blank array, just use concat or slice:

            Code:
            var myArray = [1,2,3,4,5];
            var myArrayCopy = myArray.concat();

            Comment

            Working...