Suggestion: A way to default arguments when creating functions

Completed

Comments

4 comments

  • Russell Kay

    Alex Quinn - this is a suggestion that we have internally already, it is not something we will be adding for this release but we will add it as a feature request. You can achieve this simply by taking advantage of the fact that named arguments that are not provided by the caller are set to undefined, so you can do

    function test( foo, bar ) {

     foo = (foo == undefined) ? 1 : foo;

    bar = (bar == undefined) ? 2 : bar;

     return foo + bar;

    }

     

    @QA - please add this as a feature request for tracking

    Russell

    1
    Comment actions Permalink
  • Devon Mullane

    That ... is oddly helpful.  I thought we could only expand out our arguments, not retract them in. But won't the syntax checker throw a fit if you don't provide the right number of arguments?

    1
    Comment actions Permalink
  • Russell Kay

    It will warn you, though we are going to be allowing optional arguments to be specified in the JSDoc comments which will allow this.

    The Syntax Checker is purely advisory as well

    Russell

    2
    Comment actions Permalink
  • YoYo QA Dept

    Hi Alex,

    Thank you for your feedback and sorry for the late reply.

    I have added your suggestion as a Feature Request so I'll mark this thread as Completed.

    Thanks again.

    0
    Comment actions Permalink

Please sign in to leave a comment.