2

So I have a function with quite a few parameters, for example

function myFunction(height, width, name, surname, time){
...
}

The trouble is I want to use this function to send different sets of parameters.

So sometimes it would be...

  function myFunction(height, width, depth, name, surname, time){
    ...
    }

The function is used for many tasks and I don't want a long list of parameters in the function coupled with lots of spaces in the values list when the function is called.

I need something more dynamic, ideally an associative array would be passed to the function and variables created from that.

5
  • 2
    ideally an associative array would be passed to the function give that a try. If you can't get it to work post your code. If you are using jquery you can use the extend function to merge the passed in array with a default array. Commented Apr 5, 2013 at 17:09
  • Passing an object (what you call here an "associative array") is probably best for this particular case. Commented Apr 5, 2013 at 17:12
  • I just don't know how to type the associative array literally in the function call parenthesis e?.g myFunction(["heigh">120,... is that right Commented Apr 5, 2013 at 17:13
  • @user1209203 Mohsen's answer shows the correct syntax for this. Commented Apr 5, 2013 at 17:14
  • Here's an article which you might find interesting; ejohn.org/blog/javascript-method-overloading. Although in this case using an object as an associative array is the cleaner approach, it doesn't hurt to take a look. Commented Apr 5, 2013 at 17:20

2 Answers 2

6

Because JavaScript doesn't have named arguments people usually use object as "Interfaces"

You can accept a single argument that is an object and have all those arguments as keys.

function myFunction(options){
   //access to options.height and more...

}

Pass arguments packed in an object to the function

myFunction({
  height: 100,
  widht: 300,
  ...
});
Sign up to request clarification or add additional context in comments.

2 Comments

Ok that seems to work and can I pass a function as an object value as follows, ... width:300, print: function(){ alert("done")}});
Yes you can, but be careful with scope of that function.
0

my last post was wrong. I had something different in mind. sorry for that.

you can write a function with the long lsit of paramters but you don't have to use all of them when you execute a function.

Function

function foo(a, b, c) {
    //body;
};

May be executed like this:

foo(a);
foo(a,b);
foo(a,b,c);

Inside the function check if parameters are not null and if they ar set a defaut value

function foo(a, b, c) {
    a = a !== 'null' ? a : defaultValue;
    b = b !== 'null' ? b : defaultValue;
    c = c !== 'null' ? c : defaultValue;
};

1 Comment

If a parameter isn't passed, its value isn't "null" (or null)...it's undefined.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.