Defining a function with multiple optional args

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Tom, Kukurykus

Mike Hale

Defining a function with multiple optional args

Post by Mike Hale »

Is it possible to have a function accept serveral arguments and have all the arguments opitional?

Something like this. testFunction( , ,arg, , ) where arg is the third of 5 possible arguments for this function.

I thought of using an array, but testFunction( [ , , arg, , ] ) looks funny to me and I'm afraid it might be hard to understand.

Thanks,
Mike
Andrew

Defining a function with multiple optional args

Post by Andrew »

I know of two ways you can do this.

First, not pretty but simple, pass an undefined variable for all your undefined arguments.

Code: Select alltest();
function test () {
   var u;
   alert(myfunction (u,u,2,u,u));
   
   function myfunction (a,b,c,d,e) {
      var n = 0;
      for (var i = 0; i < arguments.length; i++){
         if (isNaN(arguments)) continue;
         n = n + arguments;
      }
      return n;
   }   
}


As you can see I used myfunction's built in arguments array in the example but I need not have done, I could have tested each of a,b,c,d,e to see if they were defined or not.

The second way uses the property of the arguments array that it is generated by the calling statement (myfunction (2,3,4);) regardless of whether the variable names are defined in the function definition. This allows you to work with any number of arguments, varying with each run of the function.

Code: Select alltest();
function test () {
   var u;
   alert(myfunction (2,3,4));
   
   function myfunction () {
      var n = 0;
      for (var i = 0; i < arguments.length; i++){
         n = n + arguments;
      }
      return n;
   }   
}

Other ways to achieve much the same are to pass either a single array or an object.

Andrew
Andrew

Defining a function with multiple optional args

Post by Andrew »

To add to the above, I'd note that while the second method is tidier it is possibly more succeptible to errors since the arguments are not individually bound to a particular variable name. With the first method if you use a,b,c,d,e rather than the arguments array you do at least always know that when you specify 'c' it is 'c' that you get.

Andrew
Mike Hale

Defining a function with multiple optional args

Post by Mike Hale »

That is what I was afraid of. I had hoped that there was a way not to have to list all of the arguments. In VBA you can skip parameters you want to leave as default.

In your first example, you are still passing 5 arguments. Because your parameters and arguments are ordered, couldn't you also do this

Code: Select alltest();
function test () {
   var u;
   alert(myfunction (u,u,2,u,u));
   
   function myfunction (a,b,c,d,e) {
      if(c != undefined){
      return c; }
   }   
}

As you pointed out, you know that the thrid parameter is the third argument.

I tried using an array because I though that might be how the paramenters where passed, I can get half way there by using an array as the parameter.

Code: Select alltest();
function test () {
   var u = [,,2,,4];
   myfunction(u);
// or just myfunction([,,3,,5]);   
   function myfunction (u) {
// now use array index or array functions to handle the arguments
      for (var i = 0; i = u.length; i++){
         alert(u.shift());
      };
   };   
};

Because arrays are ordered, I know that u[2] iwould have been parameter 3,

I think that I will go with your first method. In my testing I found that you can skip var u and call the function like this myfunction ("","",3"",5). That looks as close as I can get.

I am making a set of standalone functions and I want to make them as easy to use as possible. For example, the function I have for making a curve adjustment layer is setup to use default curves is the curve for that channel is not in the parameter list. So it can take anywhere from 1 to 5 arguments.

Thanks for you help with this,
Mike
Andrew

Defining a function with multiple optional args

Post by Andrew »

As a generalisation, I would tend to prefer u to "" since undefined is not the same as an empty string. I often work with default values as follows:

Code: Select allfunction hypotenuse (a,b) {
   if (a == undefined) a = 2;
   if (b == undefined) b = 3;
   // do something eg
   a = a*a;b = b*b;
   return Math.sqrt(a + b);   
}

Sometimes I will pass a and b as properties of a single object (ie one param), sometimes I will pass them as independent variables as here.

Andrew
Mike Hale

Defining a function with multiple optional args

Post by Mike Hale »

Now that I have spent some time working with using "" as a paramenter, I see that you are right.

When using "", there is the possibility that " " could be used instead.