...

View Full Version : Pass array as function's arguments



vkidv
05-07-2006, 01:05 PM
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?

Kakao
05-07-2006, 01:55 PM
I don't know if this address your problem since you will still have an indexed object:


<html>
<head>
<script type="text/javascript">
function doThat(obj) {
alert(obj['arg1'] + obj['arg2'] + obj['arg3']);
}
function makeObj(myArray) {
obj = {};
for (var i in myArray) obj['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.

vkidv
05-07-2006, 03:45 PM
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

Kakao
05-07-2006, 04:31 PM
May be apply() is what you want:



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

vkidv
05-07-2006, 06:38 PM
This appears to work for normal functions. How about objects that are to be created with the new keyword?


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

vkidv
05-08-2006, 06:54 PM
Is this even possible?

Beagle
05-08-2006, 07:14 PM
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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum