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
    Regular Coder
    Join Date
    Sep 2002
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Pass array as function's arguments

    Hello,

    I'm looking for a way to pass an array as a function's arguments.

    Does anybody have any ideas to do this?
    I do not want to do:

    DoThat(myarray[0],myarray[1],myarray[2]) ;

    I have tried

    DoThat(myarray)
    It sees the entire contents of myarray as the first argument of the function, not desirable.

    I want the array(which is in order) to equal the functions arguments:

    Eg:

    myarray = ["param1","param2","param3"]

    function DoThat(argument1, argument2, argument3) {
    alert( argument1 + argument2 + argument3) ;
    }

    DoThat(myarray)
    should make:
    param1param2param3

    Use the contents of the array as all the arguments of the function.

    Can anybody make any suggestions?

    I know the argument is an object and not an array.. If it is how I think it works, perhaps It would be possible to write a function that structures an array as a new object with the property contents as the array data and the property names as the actual argument names of the function?
    Regards
    VKiDV

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know if this address your problem since you will still have an indexed object:
    PHP Code:
    <html>
    <
    head>
    <
    script type="text/javascript">
    function 
    doThat(obj) {
      
    alert(obj['arg1'] + obj['arg2'] + obj['arg3']);
      }
    function 
    makeObj(myArray) {
      
    obj = {};
      for (var 
    i in myArrayobj['arg' + (Number(i) + 1)] = myArray[i];
      return 
    obj;
      }
    </script>
    </head>
    <body onload="doThat(makeObj(['param1', 'param2', 'param3']));">
    </body>
    </html> 
    I don't understand the problem you are trying to solve. If you give us a clue someone could have a better solution.

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Clue? It seemed pretty self explanatory..

    Imagine a case where you would want to store parameters for a function, but lots of them..

    You would use arrays, each array would be one group of parameters for the function.

    You would then cycle through each array and feed the array's contents as the function's parameters.

    params = [
    ["yes","no"],
    ["on","off"]
    ]

    function Opp(Positive, Negative) {
    alert("The opposite of " + Positive + " is " + Negative) ;
    }

    Opp(params[0]) ;

    That example is simplified(practically useless) but hopefully you get the idea: Use the array as an input to the function
    Regards
    VKiDV

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    May be apply() is what you want:

    PHP Code:
    <html>
    <
    head>
    <
    script type="text/javascript">
    params = [
    [
    "yes","no"],
    [
    "on","off"]
    ];
    function 
    Opp(PositiveNegative) {
      
    alert("The opposite of " Positive " is " Negative) ;
      }
    </script>
    </head>
    <body onload="Opp.apply(null, params[0]);">
    </body>
    </html> 

  • #5
    Regular Coder
    Join Date
    Sep 2002
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This appears to work for normal functions. How about objects that are to be created with the new keyword?

    Code:
    <html>
    <head>
    <script type="text/javascript">
    params = [
    ["yes","no"],
    ["on","off"]
    ];
    function Opp(Positive, Negative) {
      alert("The opposite of " + Positive + " is " + Negative) ;
      }
    
    function theobject(param1,param2) {
     this.param1 = param1 ;
     this.param2 = param2 ;
    alert(this.param1 + this.param2) ;
    }
    
    </script>
    </head>
    <body onload="Opp.apply(null, params[0]);d=theobject.apply(null,params[0]);alert(d.param1);">
    </body>
    </html>
    I get alerted 'yesno' as expected, but the last results in an error.
    When I put the 'new ' infront of theobject, I also get an error.

    I think it is because the apply function doesn't return the newly created object.
    Last edited by vkidv; 05-07-2006 at 06:43 PM.
    Regards
    VKiDV

  • #6
    Regular Coder
    Join Date
    Sep 2002
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is this even possible?
    Regards
    VKiDV

  • #7
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Code:
    function opposites(pos, neg)
    {
      if (typeof pos == "object")
        alert("The opp of " + pos[0] + " is " + pos[1]);
      else
       alert("The opp of " + pos + " is " + neg);
    }
    Does this satisfy your needs?


  •  

    Posting Permissions

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