Is it possible to initialize function parameters in Javascript?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hiroshi Ochi

    #1

    Is it possible to initialize function parameters in Javascript?

    Hello,

    Is it possible to initialize javascript function parameters (using MSIE
    6.0 and above)? According to the link below, it seems possible to do
    this.



    Why I keep getting error with the below code?

    <!-- start sample code -->
    <html>
    <script language="javas cript">
    function myFunc(paramOne ,paramTwo="This is Default Message"){
    alert('First Parameter is '+ paramOne );
    alert('Second Parameter is '+ paramTwo );
    }

    <!-- first call -->
    myFunc("My First Parameter message","My Second Parameter
    Message");
    <!-- second call -->
    myFunc("My First Parameter message");


    </script>
    </html>
    <!-- end sample code -->

    I am expecting it to show the 'This is Default Message' in the second
    call.

    Would somebody give me a hint?

    Thanks,
    hiroshi




    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Robert

    #2
    Re: Is it possible to initialize function parameters in Javascript?

    In article <40a31071$0$202 $75868355@news. frii.net>,
    Hiroshi Ochi <hiroshi_ochi@n ospam.com> wrote:
    [color=blue]
    > Hello,
    >
    > Is it possible to initialize javascript function parameters (using MSIE
    > 6.0 and above)? According to the link below, it seems possible to do
    > this.
    >
    > http://www.mozilla.org/js/language/j...ale/named.html
    >[/color]

    You'll note that this is a JavaScript 2.0 reference. I believe that IE
    6.0 implements Javascript 1.5

    Robert

    Comment

    • Hiroshi Ochi

      #3
      Re: Is it possible to initialize function parameters in Javascript?

      Hi Robert,

      Hmm, too bad. I think I'll just do the below workaround:

      <!-- start sample code -->
      <html>
      <script language="javas cript">
      function myFunc(paramOne ,paramTwo){
      if(paramTwo == undefined)
      paramTwo = someValue; // initialize default value
      ...
      }
      </script>
      </html>
      <!-- end sample code -->

      thanks,
      hiroshi

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Saint Jude

        #4
        Re: Is it possible to initialize function parameters in Javascript?

        Hiroshi Ochi <hiroshi_ochi@n ospam.com> wrote in message news:<40a42507$ 0$202$75868355@ news.frii.net>. ..[color=blue]
        > Hi Robert,
        >
        > Hmm, too bad. I think I'll just do the below workaround:
        >
        > <!-- start sample code -->
        > <html>
        > <script language="javas cript">
        > function myFunc(paramOne ,paramTwo){
        > if(paramTwo == undefined)
        > paramTwo = someValue; // initialize default value
        > ...
        > }
        > </script>
        > </html>
        > <!-- end sample code -->[/color]

        Comparing against undefined *may* be risky, and involve backward
        compatibility problems. I'd go for: if(!paramTwo) - for an undefined
        parameter test.

        In fact JS has a good shortcut for your purpose:

        if(paramTwo == undefined)
        paramTwo = someValue; // initialize default value

        becomes

        paramTwo = paramTwo || someValue

        Comment

        • Lee

          #5
          Re: Is it possible to initialize function parameters in Javascript?

          Saint Jude said:
          [color=blue]
          >Comparing against undefined *may* be risky, and involve backward
          >compatibilit y problems. I'd go for: if(!paramTwo) - for an undefined
          >parameter test.
          >
          >In fact JS has a good shortcut for your purpose:
          >
          > if(paramTwo == undefined)
          > paramTwo = someValue; // initialize default value
          >
          >becomes
          >
          > paramTwo = paramTwo || someValue[/color]


          I doubt that undefined would be a problem with any
          implementation of Javascript. On the other hand,
          both of your methods assume that zero, false, and
          "" are not valid parameter values.

          Comment

          • Richard Cornford

            #6
            Re: Is it possible to initialize function parameters in Javascript?

            Lee wrote:[color=blue]
            > Saint Jude said:[color=green]
            >>Comparing against undefined *may* be risky, and involve backward
            >>compatibili ty problems. I'd go for: if(!paramTwo) - for an undefined
            >>parameter test.
            >>
            >>In fact JS has a good shortcut for your purpose:
            >>
            >> if(paramTwo == undefined)
            >> paramTwo = someValue; // initialize default value
            >>
            >>becomes
            >>
            >> paramTwo = paramTwo || someValue[/color]
            >
            >
            > I doubt that undefined would be a problem with any
            > implementation of Javascript. On the other hand,
            > both of your methods assume that zero, false, and
            > "" are not valid parameter values.[/color]

            IE 4 doesn't like it; it produces the extremely helpful error message
            "undefined is undefined." :)

            But there is no need to risk the problem as - undefined - can be
            implemented in unsupporting environments with:-

            this.undefined = this.undefined;

            -or:-

            window.undefine d = window.undefine d;

            - executed as inline code as the page loads.

            Recent discussion on the identity operator - === - suggest that it has
            probably moved into the set of more recent javascript features that can
            now universally be relied upon as both Netscape 4 and IE 4 understand it
            and they are the generation of browsers apparently now dropping out of
            use. The identity operator doesn't type convert so identity comparison
            with a normalised - undefined - should give an unambiguous result:

            if(paramTwo === undefined){
            ... //paramTwo needs defaulting
            }

            - on current browsers without the risk of errors. But a - typeof - test
            (if slower) should also produce a reliable result:-

            if(typeof paramTwo == "undefined" ){
            ... //paramTwo needs defaulting
            }

            - without any real language version concerns as typeof has been around
            since JavaScript 1.1.

            Richard.


            Comment

            • Grant Wagner

              #7
              Re: Is it possible to initialize function parameters in Javascript?

              Lee wrote:
              [color=blue]
              > Saint Jude said:
              >[color=green]
              > >Comparing against undefined *may* be risky, and involve backward
              > >compatibilit y problems. I'd go for: if(!paramTwo) - for an undefined
              > >parameter test.
              > >
              > >In fact JS has a good shortcut for your purpose:
              > >
              > > if(paramTwo == undefined)
              > > paramTwo = someValue; // initialize default value
              > >
              > >becomes
              > >
              > > paramTwo = paramTwo || someValue[/color]
              >
              > I doubt that undefined would be a problem with any
              > implementation of Javascript. On the other hand,
              > both of your methods assume that zero, false, and
              > "" are not valid parameter values.[/color]

              Which is why it is probably best to test for precisely what the parameter
              should be:

              if (typeof paramTwo != 'string') {
              paramTwo = 'default string';
              }

              - or -

              if (typeof paramTwo != 'number') {
              paramTwo = 0;
              }

              etc

              --
              | Grant Wagner <gwagner@agrico reunited.com>

              * Client-side Javascript and Netscape 4 DOM Reference available at:
              *


              * Internet Explorer DOM Reference available at:
              *
              http://msdn.microsoft.com/workshop/a...ence_entry.asp

              * Netscape 6/7 DOM Reference available at:
              * http://www.mozilla.org/docs/dom/domref/
              * Tips for upgrading JavaScript for Netscape 7 / Mozilla
              * http://www.mozilla.org/docs/web-deve...upgrade_2.html


              Comment

              • Dr John Stockton

                #8
                Re: Is it possible to initialize function parameters in Javascript?

                JRS: In article <40a31071$0$202 $75868355@news. frii.net>, seen in
                news:comp.lang. javascript, Hiroshi Ochi <hiroshi_ochi@n ospam.com> posted
                at Thu, 13 May 2004 06:06:42 :
                [color=blue]
                >Is it possible to initialize javascript function parameters (using MSIE
                >6.0 and above)?[/color]
                [color=blue]
                >function myFunc(paramOne ,paramTwo="This is Default Message"){
                > ...[/color]

                Be aware that one can do something like

                function myFunc(paramOne , paramTwo) {
                if (!paramTwo) paramTwo = "This is Default Message"
                ....

                But consider whether one might need to be able to supply such as 0,
                false, NaN, ... for paramTwo.

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                Comment

                • Lasse Reichstein Nielsen

                  #9
                  Re: Is it possible to initialize function parameters in Javascript?

                  Lee <REM0VElbspamtr ap@cox.net> writes:
                  [color=blue]
                  > I doubt that undefined would be a problem with any
                  > implementation of Javascript.[/color]

                  In later versions of Javascript, "undefined" is merely a global
                  variable. In, e.g., IE 5, that variable was not defined, so writing
                  if (blah == undefined) ...
                  would give the undeclared variable error:
                  'undefined' is undefined
                  Kindof cute, really :)

                  /L
                  --
                  Lasse Reichstein Nielsen - lrn@hotpop.com
                  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                  'Faith without judgement merely degrades the spirit divine.'

                  Comment

                  • Saint Jude

                    #10
                    Re: Is it possible to initialize function parameters in Javascript?

                    Lee <REM0VElbspamtr ap@cox.net> wrote in message news:<c82kcj043 u@drn.newsguy.c om>...[color=blue]
                    > Saint Jude said:
                    >[color=green]
                    > >Comparing against undefined *may* be risky, and involve backward
                    > >compatibilit y problems. I'd go for: if(!paramTwo) - for an undefined
                    > >parameter test.
                    > >
                    > >In fact JS has a good shortcut for your purpose:
                    > >
                    > > if(paramTwo == undefined)
                    > > paramTwo = someValue; // initialize default value
                    > >
                    > >becomes
                    > >
                    > > paramTwo = paramTwo || someValue[/color]
                    >
                    >
                    > I doubt that undefined would be a problem with any
                    > implementation of Javascript. On the other hand,
                    > both of your methods assume that zero, false, and
                    > "" are not valid parameter values.[/color]

                    Lee, you are absolutely right about zero, false etc. I realised while
                    doing my laundry. That syntax is unfortunately unsafe to use.

                    Re: undefined

                    I'm not so sure about this. Perhaps you can put me straight.
                    According to MS JScript specification, undefined requries
                    JScript 5.5 (IE 5.5+). So I would suggest:
                    if(typeof(arg)= ="undefined" )
                    instead.

                    BTW, which is the safer to use - typeof operator, or typeof function ?

                    Comment

                    • Lasse Reichstein Nielsen

                      #11
                      Re: Is it possible to initialize function parameters in Javascript?

                      thejuliankenned y@hotmail.com (Saint Jude) writes:[color=blue]
                      > BTW, which is the safer to use - typeof operator, or typeof function ?[/color]

                      There is no typeof function, only the operator.
                      Writing
                      typeof foo
                      and
                      typeof(foo)
                      is equivalent, just as
                      1+4
                      and
                      1+(4)
                      are. It's just a parenthesis around an expression.

                      /L
                      --
                      Lasse Reichstein Nielsen - lrn@hotpop.com
                      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                      'Faith without judgement merely degrades the spirit divine.'

                      Comment

                      • Lee

                        #12
                        Re: Is it possible to initialize function parameters in Javascript?

                        Saint Jude said:
                        [color=blue]
                        >I'm not so sure about this. Perhaps you can put me straight.
                        >According to MS JScript specification, undefined requries
                        >JScript 5.5 (IE 5.5+). So I would suggest:
                        >if(typeof(arg) =="undefined" )
                        >instead.[/color]

                        Yes, that's actually what I always use. I posted in haste.

                        Comment

                        • Dr John Stockton

                          #13
                          Re: Is it possible to initialize function parameters in Javascript?

                          JRS: In article <c82kcj043u@drn .newsguy.com>, seen in
                          news:comp.lang. javascript, Lee <REM0VElbspamtr ap@cox.net> posted at Fri,
                          14 May 2004 07:16:51 :[color=blue]
                          >Saint Jude said:
                          >[color=green]
                          >>Comparing against undefined *may* be risky, and involve backward
                          >>compatibili ty problems. I'd go for: if(!paramTwo) - for an undefined
                          >>parameter test.
                          >>
                          >>In fact JS has a good shortcut for your purpose:
                          >>
                          >> if(paramTwo == undefined)
                          >> paramTwo = someValue; // initialize default value
                          >>
                          >>becomes
                          >>
                          >> paramTwo = paramTwo || someValue[/color]
                          >
                          >
                          >I doubt that undefined would be a problem with any
                          >implementati on of Javascript. On the other hand,
                          >both of your methods assume that zero, false, and
                          >"" are not valid parameter values.[/color]

                          Not quite; if one of them is a valid value then it can be the default.

                          P2 = 0
                          P2 = P2 || 0

                          results in P2 becoming the second 0 & no longer the first one, an
                          undetectable assignment.


                          ISTM that, in IE4,
                          X |= Y is X = X | Y
                          but X ||= Y is illegal. I wonder why.

                          --
                          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                          <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                          Comment

                          • Steve van Dongen

                            #14
                            Re: Is it possible to initialize function parameters in Javascript?

                            Hiroshi Ochi <hiroshi_ochi@n ospam.com> wrote:
                            [color=blue]
                            >Hi Robert,
                            >
                            >Hmm, too bad. I think I'll just do the below workaround:
                            >
                            ><!-- start sample code -->
                            ><html>
                            ><script language="javas cript">
                            >function myFunc(paramOne ,paramTwo){
                            > if(paramTwo == undefined)
                            > paramTwo = someValue; // initialize default value
                            > ...
                            >}
                            ></script>
                            ></html>
                            ><!-- end sample code -->[/color]

                            x == undefined evaluates to true if x is null. Use typeof paramTwo ==
                            "undefined" if that matters to you.

                            Regards,
                            Steve

                            Comment

                            Working...