Default function parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Börni

    Default function parameter

    Hi,

    Is there a way to provide a default for a function parameter.
    i tried
    function func (type, message, obj = "message")
    but it doesn't seem to work.
    It seems a bit ugly to make an if/else statement right at the begining
    of a function, just to imitate this behavior.
  • Ulrik Skovenborg

    #2
    Re: Default function parameter

    Börni wrote:[color=blue]
    > Is there a way to provide a default for a function parameter.
    > i tried
    > function func (type, message, obj = "message")
    > but it doesn't seem to work.[/color]

    I usually use this way to make default parameters:
    function func (type, message, obj) {
    obj = (typeof obj == "undefined" ) ? "message" : obj;

    or just
    obj = (!obj) ? "message" : obj;
    but I think IE5 (or whatever) has a problem with that.

    Comment

    • Michael Winter

      #3
      Re: Default function parameter

      On Thu, 23 Dec 2004 20:20:31 +0000, Börni <b.reiter@onlin ehome.de> wrote:
      [color=blue]
      > Is there a way to provide a default for a function parameter.[/color]

      Typically, this is done with:

      function myFunction(arg) {arg = arg || ???;
      /* ... */
      }

      where ??? is your default value.

      The logical OR operator is a bit different in ECMAScript when compared to
      other languages. Normally,

      opr1 || opr2

      would evaluate to true or false, depending on whether either opr1 or opr2
      is true. In ECMAScript, the operands are tested for true- or "falseness" ,
      but it's their values that are actually returned. Consider:

      false || 2

      The literal, false, will obviously evaluate to false so the result of the
      expression is 2. With:

      'hello' || 'world'

      the non-empty string, 'hello', will evaluate to true so the result of the
      expression is 'hello'.

      If code called the function above with an argument that evaluated to false
      (null, undefined, false, 0, or ''), or no argument at all, the second
      operand would be assigned to arg. Of course, if one of those values is a
      legal argument that shouldn't be changed, this particular approach isn't
      suitable. In that case, you can use Ulrik's first suggestion.

      [snip]

      Hope that helps,
      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Douglas Crockford

        #4
        Re: Default function parameter

        > Is there a way to provide a default for a function parameter.[color=blue]
        > i tried
        > function func (type, message, obj = "message")
        > but it doesn't seem to work.[/color]

        Of course it doesn't work. You can't make up your own syntax.

        You can use the default operator.

        a || b

        If a is falsy (null, undefined, 0, or empty string) then the value is a.
        Otherwise it is b. Missing parameters get the value undefined, which is
        falsy.


        Comment

        • Ivo

          #5
          Re: Default function parameter

          "Michael Winter" said[color=blue]
          > Börni asked[color=green]
          >> Is there a way to provide a default for a function parameter[/color]
          >
          > Typically, this is done with:
          >
          > function myFunction(arg) {arg = arg || ???;
          > /* ... */
          > }
          >
          > where ??? is your default value.
          >[/color]

          Probably the shortest syntax is:

          function myFunction(arg) { arg |= ???;

          When no arg is provided, it will be of type 'undefined'. To catch the
          occasion when it may actually be false, null, an empty string or zero, you
          need to include a typeof test.

          HTH
          --
          Ivo



          Comment

          • Michael Winter

            #6
            Re: Default function parameter

            On Fri, 24 Dec 2004 01:54:05 +0100, Ivo <no@thank.you > wrote:

            [snip]
            [color=blue]
            > Probably the shortest syntax is:
            >
            > function myFunction(arg) { arg |= ???;[/color]

            It might be the shortest syntax, but it's not very appropriate. That
            operation is performed in terms of 32-bit integers only. Moreover,
            consider when the operand is another number other than zero. If arg is
            zero, the default will be set to the operand as expected, but if arg is
            non-zero it will be assigned the value of (arg | operand), which will
            mutate the argument in what is probably an undesirable way.

            Unfortunately, the production

            arg ||= def;

            doesn't exist.

            [snip]

            Mike

            --
            Michael Winter
            Replace ".invalid" with ".uk" to reply by e-mail.

            Comment

            • Börni

              #7
              Re: Default function parameter

              [color=blue][color=green]
              >> function myFunction(arg) {arg = arg || ???;
              >> /* ... */
              >> }[/color][/color]

              I took the above approach as i can be sure that i never will pass an
              argument that contains 0, undefined...
              ( it gets passed the id of an html element )

              Comment

              • Ivo

                #8
                Re: Default function parameter

                "Ivo" wrote[color=blue]
                > "Michael Winter" said[color=green]
                > > Börni asked[color=darkred]
                > >> Is there a way to provide a default for a function parameter[/color]
                > >
                > > Typically, this is done with:
                > >
                > > function myFunction(arg) {arg = arg || ???;
                > > /* ... */
                > > }
                > >
                > > where ??? is your default value.
                > >[/color]
                >
                > Probably the shortest syntax is:
                >
                > function myFunction(arg) { arg |= ???;
                >
                > When no arg is provided, it will be of type 'undefined'. To catch the
                > occasion when it may actually be false, null, an empty string or zero, you
                > need to include a typeof test.[/color]

                Slightly related, to remember the arg used last time the function was
                called, but without creating a global variable, use "this". In the following
                example, the function "myFunc" gets its argument "p" from:
                1. where ever it is called from, or -if called without argument-
                2. a prompt box, with a default value of:
                1. the argument used last time myFunc ran, or -if this is the first time-
                2. 'myArgument'.

                function myFunc(p) {
                p= p || window.prompt( 'Enter your argument:', this.last ||
                'myArgument' );
                if(p) {
                this.last = p;
                /* ... */
                }
                }

                // this will invoke prompt box:
                someElement.onc lick = myFunc;

                // this will use 'someargument' :
                someotherElemen t.onclick = function() { myFunc( 'someArgument' ); };

                Other functions may feature their own "this.last" history without
                interference. It works for me, although I don't know whether "this" is the
                calling element or the function scope itself...
                --
                Ivo


                Comment

                • Michael Winter

                  #9
                  Re: Default function parameter

                  On Fri, 24 Dec 2004 13:58:39 +0100, Ivo <no@thank.you > wrote:

                  [snip]
                  [color=blue]
                  > // this will invoke prompt box:
                  > someElement.onc lick = myFunc;
                  >
                  > // this will use 'someargument' :
                  > someotherElemen t.onclick = function() { myFunc( 'someArgument' ); };
                  >
                  > Other functions may feature their own "this.last" history without
                  > interference. It works for me, although I don't know whether "this" is
                  > the calling element or the function scope itself...[/color]

                  It depends how myFunc is called. With the first approach where myFunc is,
                  and will be called as, a method of someElement, this will refer to that
                  element. With the second where myFunc is called directly, this will refer
                  to the global object.

                  Mike

                  --
                  Michael Winter
                  Replace ".invalid" with ".uk" to reply by e-mail.

                  Comment

                  Working...