JS: Default value for function arg: Max Console complains...

darrcass's icon

Function:

function vkeys(d,p=-1){
    return p>-1 ? p+(d*maxt()) : d*maxt();
}

Everything works as expected, but Max sez:

js: custemp.js: Javascript SyntaxError: missing ) after formal parameters, line 68
js: source line: function vkeys(d,p=-1){

How does Max (7.0.4) want me to do this?

Jan M's icon

That has nothing to do with Max. JavaScript syntax does not this kind of declaration.
Try:

function foo(bar)
{
    bar = bar || 1;
   // short for;
   // if(!bar) {
   //       bar = 1;
   // }   
}

darrcass's icon

Thanks.

Emmanuel Jourdan's icon

In case you're wondering Max 7 doesn't support ECMA Script 6's default arguments at this point. You can either check if the argument is equal to "undefined", or check the arguments array:

function toto(a, b)
{
    if (a === "undefined")
        a = 42; // your default value for a
    if (b === "undefined")
        b = 74;    // your default value for b
}

or

function titi(a, b)
{
    if (arguments.length < 1)
        a = 42; // your default value for a
    if (arguments.length < 2)
        b = 74;    // your default value for b
}