Sure its been asked but cant find anything, can we do optional parameters/default value in scripts?
I think you are referring to passing variables to functions in a script?
In JavaScript (and JScript implementation in SambaPOS), you can have “optional” parameters in functions, and if you don’t specify them, they are “undefined”.
As for default values, you can just define a variable with var
then put = "default value"
or whatever when defining the variable.
This test script should illustrate it all:
function test(var1, var2) {
var var3 = "default value";
return var1;
}
Changing the return value between the 3 variables (this is the best way to show the exact output as using dlg.ShowMessage
or any manipulation of the output will throw an exception with “undefined” variables:
You can of course test for optional parameters quite easily:
function test(var1, var2) {
var var3 = "default value";
if (var2) {
return true;
}
else {
return false;
}
}
FFS, was being an idiot, thanks mark, think I was fixated on the php method of putting var3=’’ in the function parameters.
In short, yes, simple, script treats declared parameters as existing but undefined by default.
Out of practice with all this LOL
Actually in JavaScript you even don’t need to declare variables. This is one of the disadvantages of JavaScript as well. It’s just a best practice that developers will put var
declarations at the top or before using a variable, but it is not needed. So you can easily do this without any declaration or issue:
x = 1;
y = x + 1;