method overloading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Smith

    #1

    method overloading

    can we overload a javascript function with different argument?

    example:

    function a(a){}
    function a(a,b){}


  • Janwillem Borleffs

    #2
    Re: method overloading

    John Smith wrote:[color=blue]
    > can we overload a javascript function with different argument?
    >
    > example:
    >
    > function a(a){}
    > function a(a,b){}[/color]

    See for yourself:

    <script type="text/javascript">
    function doalert(a, b) {
    alert('doalert# 1 called');
    }

    function doalert(a) {
    alert('doalert# 2 called');
    }

    doalert(1,2);
    </script>

    You will see that the second definition of the doalert() function overrides
    the first one, so the answer is no.

    An alternative would be to use a variable number of arguments when calling a
    function and decide what to do based upon the defined arguments.


    JW



    Comment

    • Michael Winter

      #3
      Re: method overloading

      On 28/03/2005 09:31, John Smith wrote:
      [color=blue]
      > can we overload a javascript function with different argument?
      >
      > example:
      >
      > function a(a){}
      > function a(a,b){}[/color]

      Not in the way other languages define method overloading, no. In the
      case above, the function object created by the first function
      declaration would immediately be replaced by the second.

      There are a few approaches that can be useful, depending on what
      you're trying to achieve. You give no details so I'll provide a quick
      run through them and you can determine which is best. I'll apologise
      now for the exceedingly artifical examples. :/

      1) Optional arguments

      If a method is defined as taking optional arguments, you can add
      the defaults by taking advantage of ECMAScript's rather different
      logical OR (||) operator:

      function myFunction(a, b) {
      b = b || 'default';
      }

      If the argument, b, evaluates to false (that is, zero (0), empty
      string (''), null, undefined, or false), the second operand will
      be assigned in its place.

      If one of these "false" values is legal, but not the default, see
      (3) for an alternative.

      2) Overloading by number of arguments

      The arguments object available within all functions contains all
      arguments passed to the function as properties, and the number of
      arguments in its length property. You can use the value of the
      latter to change the behaviour of the function, and the former to
      access any arguments that do not have a corresponding formal
      identifier.

      function myFunction() {var msg;
      if(arguments.le ngth) {
      msg = arguments.lengt h + ' arguments passed:\n';

      for(var i = 0, n = arguments.lengt h; i < n; ++i) {
      msg += '\n' + arguments[i];
      }
      } else {
      msg = 'No arguments passed!';
      }
      alert(msg);
      }

      3) Overloading by type of arguments

      I sometimes use this approach when I want to allow a function to
      take either a reference to an element, or its id attribute value.
      You can examine the type of the argument using the typeof
      operator:

      function myFunction(elem ent) {
      if('string' == typeof element) {
      element = document.getEle mentById(elemen t);
      }
      if(element) {
      /* ... */
      }
      }

      If a function is called and an argument doesn't have a corresponding
      value, it will be undefined with typeof evaluating to the string,
      'undefined'. Returning to the example in (1), you might have an
      argument which expects an integer. If the argument was unspecified,
      you might want it to assume the value -1, but zero is legal. In that
      case,

      function myFunction(a, b) {
      if('undefined' == typeof b) {b = -1;}
      }


      Hope that helps,
      Mike

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

      Comment

      • Dr John Stockton

        #4
        Re: method overloading

        JRS: In article <JwV1e.7117$Ab. 3181@text.news. blueyonder.co.u k>, dated
        Mon, 28 Mar 2005 15:37:13, seen in news:comp.lang. javascript, Michael
        Winter <m.winter@bluey onder.co.invali d> posted :[color=blue]
        > 1) Optional arguments
        >
        > If a method is defined as taking optional arguments, you can add
        > the defaults by taking advantage of ECMAScript's rather different
        > logical OR (||) operator:
        >
        > function myFunction(a, b) {
        > b = b || 'default';
        > }
        >
        > If the argument, b, evaluates to false (that is, zero (0), empty
        > string (''), null, undefined, or false), the second operand will
        > be assigned in its place.[/color]

        That means that if the default is not zero, etc., it is impossible to
        give and get used a parameter which is zero, etc.

        How about if (b==null) b = 'default' ? It should enable having a
        default which is not zero, etc., and giving p parameter which is zero,
        an empty string, undefined, or false - although not for a value which,
        like that of var U , is undefined, which needs b===null .

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of 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

        • Michael Winter

          #5
          Re: method overloading

          On 28/03/2005 21:20, Dr John Stockton wrote:

          [MLW:][color=blue][color=green]
          >> function myFunction(a, b) {
          >> b = b || 'default';
          >> }[/color][/color]

          [snip]
          [color=blue]
          > That means that if the default is not zero, etc., it is impossible to
          > give and get used a parameter which is zero, etc.[/color]

          I thought I made that point (or at least a similar one) at the end of
          that section?
          [color=blue]
          > How about if (b==null) b = 'default' ?[/color]

          My later suggestion was the use of the typeof operator that, whilst
          slower than a equality test with null, should be foolproof for all
          situations.
          [color=blue]
          > not for a value which, like that of var U , is undefined, which needs b===null .[/color]

          As null and undefined are of different types, undefined !== null.
          You'd either want to use typeof or a strict comparison with an
          uninitialised variable. Of course, if you don't care about early
          browsers and pre-IE 5.5 (not likely), you could also use the undefined
          keyword.

          Mike

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

          Comment

          Working...