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 4 of 4
  1. #1
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts

    Question passing a collection as parameter

    is it possible to pass a whole collection of objects as a parameter?

    function bla(){
    var col = new Array()
    ..build the colection
    bla2(col);
    ..continue with the new collection
    col[0]... and so on....
    }

    function bla2(col){
    ...do something with the collection col which becomes newcol
    this.col=newcol;
    return this.col;
    }

    Or it is better to use a new Function() to do that?
    Last edited by Kor; 03-18-2005 at 04:15 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    You mean like this?
    Code:
    function one(){
        var
            aTest=[1,2,3];
        aTest.push(4);
        two(aTest);
        aTest.push(5);
        alert('aTest: '+aTest);
    }
    
    function two(aOrig){
        var
            aCopy=[].concat(aOrig).reverse();
        aCopy.push(0);
        alert('aCopy: '+aCopy);
    }
    
    one();
    Yes, you can do that. Be aware of the difference between that way of doing it and what happens if you change aCopy=[].concat(aOrig).reverse(); to aCopy=aOrig.reverse();. Arrays are reference types, and copying a reference type doesn't mean the creation of an exact replica, only the copying of a reference to the same object. If you modify the array, you will change the object that references point to, unless you make sure a new object is created.


    Also, in JavaScript arrays and collections are considerably different things. Collections are dense, immutable, automatically sized and filled, and doesn't inherit from the Array object. Arrays are sparse (the length may be greater than the number of elements in it), mutable, dynamically sized and manually filled, and do inherit from the Array object. Collections are unlike arrays not natives of the language, but are provided by host implementations of the DOM; e.g. NodeList, NamedNodeMap, HTMLCollection, HTMLOptionsCollection.
    Last edited by liorean; 03-18-2005 at 05:14 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
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    ...so that I would better re-create the collection as new Object() ?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Well, unless you actually want to be editing *the same* array, you need to copy it. A line like aCopy=aOrig.concat([]); does just this. Beware that this makes a shallow copy and not a deep copy. If you want to create a deep copy, see my Array Functionality script.
    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


  •  

    Posting Permissions

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