Default parameters in functions
- This topic has 3 replies, 3 voices, and was last updated 4 years, 2 months ago by .
- You must be logged in to reply to this topic.
Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Default parameters in functions
Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Default parameters in functions
Does ExtendScript not support default parameters in functions? i.e.:
function doSomething (a, b = null, c = 100) { // do scripty things }
I can’t get defaults to work that way, and the “old” workaround I used to use inside the function doesn’t seem to work either:
function doSomething (a, b, c) {
b = typeof b !== ‘undefined’ ? b : null;
c = typeof c !== ‘undefined’ ? c : 100;
// do scripty things
}
Thanks in advance.
I believe ExtendScript is based on ye olde ES3, while default parameters are a feature of ES6, so you’re out of luck I’m afraid.
As for your workaround, that should work in JavaScript, but ExtendScript can be a strange and unpredictable beast at times. In your sample code there, I notice you have ‘smart’ quotes around ‘undefined’, but I’m guessing that’s not in your actual code? You could try testing your variables directly against undefined instead:
function doSomething (a, b, c) {
b = (b !== undefined) ? b : null;
c = (c !== undefined) ? c : 100;
// do scripty things
}
I can’t get defaults to work that way, and the “old” workaround I used to use inside the function doesn’t seem to work either:
I can’t get defaults to work that way https://tutuapp.uno/ https://9apps.ooo/ https://showbox.kim/ , and the “old” workaround I used to use inside the function doesn’t seem to work either:
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies allows us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.
You have to be logged in to save.
Not a member? Join Today - it's FREE!