Default parameters in functions

Home Forums InDesign Add-ons (Scripts, Scripting, and Plug-ins) Default parameters in functions

Tagged: 

This topic contains 3 replies, has 3 voices, and was last updated by  druparker 1 month, 1 week ago.

  • Author
    Posts
  • #122860

    Ward Baxter
    Member

    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.

  • #122864

    Kal Starkis
    Member

    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
    }
  • #122944

    druparker
    Member

    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:

  • #122947

    druparker
    Member

    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:

You must be logged in to reply to this topic.