Reputation: 1981
I have a function which has input parameters as below:
function a(s,d,f,r,g,h,u)
I want to combine all the input parameters into one object and pass it in to the function. I have to write a set function instead of using options. Any idea regarding this will be really helpful.
Upvotes: 0
Views: 1873
Reputation: 6003
...rather than passing the object that you'll create, you could make use of the default arguments
parameter available to all functions and go from there.
function a(){alert(arguments.length)}
a(1,2); // alerts 2
a(1,2,3); // alerts 3
a(1); // alerts 1
Hope this helps.
Upvotes: 0
Reputation: 3860
you can create an array of input parameters and can pass to function
var array = new Array()
array[0] = parameter1
array[1] = parameter2
function(array){
//use parameters here like array[0]
}
Upvotes: 0
Reputation: 9447
If you want to pass one argument to the function then use
function fn(arr) { }
var arr = [s,d,f,r,g,h,u];
fn(arr);
Alternatively, If you want to pass the same number of arguments but still use one variable for it then use
function fn (s,d,f,r,g,h,u) { }
var arr = [s,d,f,r,g,h,u];
fn.call(null, arr);
fn.call
It will pass the elements of the array as the parameters of the function
Upvotes: 0
Reputation: 11958
use an array: [s,d,f,r,g,h,u]
or make an object: {s: s,d: d,f: f,r: r,g: g,h: h,u: u}
function a(obj)
{
...
}
in your function you can access the members of an array by this way:
obj[number]
e.g. obj[0]
will be the s
, obj[3]
will be the r
and if you need to make an object you can access the elements just writing obj.name
e.g. obj.d
will be the d
and obj.h
will be the h
Upvotes: 1