Method Overloading with JavaScript

Last post 11-22-2006 5:53 PM by bleroy. 4 replies.

Sort Posts:

  • Method Overloading with JavaScript

    11-18-2006, 7:53 PM
    • Contributor
      6,263 point Contributor
    • CyrilCS
    • Member since 08-16-2006, 9:48 PM
    • DURAND
    • Posts 21

    Hi,

    I haven't seen Method Overloading when I watch the MicrosoftAjax.js it is possible but I don't have an "official method" to do this. What do you think abut my method ? I think it will be interesting to have this feature in Visual Studio "Orcas".

    // Création d'un namespace

    Type.registerNamespace('Sample');

     

    // Constructeur de la class Personne

    Sample.Person = function(firstName, lastName){

        this._firstName = firstName;

        this._lastName = lastName;

    }

     

    // Rajout des différentes méthodes à notre type

    Sample.Person.prototype = {

        get_firstName : function(){

            return this._firstName;

        },

        set_firstName : function(value){

            this._firstName = value;

        },

        get_lastName : function(){

            return this._lastName;

        },

        set_lastName : function(value){

            this._lastName = value;

        },

        toString : function(){

            if (arguments.length == 1 && Object.getType(arguments[0]) == String){

                return this.toString$String.apply(this, arguments);

            } else if (arguments.length == 2 && Object.getType(arguments[0]) == String && Object.getType(arguments[1]) == Sample.Person){

                return this.toString$String$Person.apply(this, arguments);

            } else {

                return this.toString$.apply(this, arguments);

            }

        },

        toString$ : function(){

            return String.format('je suis {0} {1}', this.get_firstName(), this.get_lastName());

        },

        toString$String : function(format){

            return String.format(format, this.get_firstName(), this.get_lastName());

        },

        toString$String$Person : function(format, person){

            return String.format('{0} , {1}', this.toString(format), person.toString(format));

        }

    }

     

    // Enregistrement de notre type dans le framework Atlas

    Sample.Person.registerClass('Sample.Person');

     

    and we use it like this :

        window.pageLoad = function(){

            var p = new Sample.Person('Cyril', 'Durand');

           

            alert(p.toString());

            alert(p.toString('{0} {1}'));

           

            var p2 = new Sample.Person('Toto', 'Bidule');

            alert(p.toString('{0} {1}', p2));

        }

     

    Filed under: , , ,
  • Re: Method Overloading with JavaScript

    11-19-2006, 6:54 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs

    hello.

    i'd say that this isn't possible. when you define the 2nd method, it'll just "replace" thes 1st without complaining. so, i think this is just one of those things that we won't have. 

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: Method Overloading with JavaScript

    11-19-2006, 8:04 AM
    • Contributor
      6,263 point Contributor
    • CyrilCS
    • Member since 08-16-2006, 9:48 PM
    • DURAND
    • Posts 21

    My sample works ! I have 4 methods : toString , toString$, toString$String and toString$String$Person no method will be replaced. Wen we call toString('{0}, {1}'); the toString method will be execute it analyze the type of the arguments and call toString$String methods.

    My question is what do you think about this method ? In my project I need it and I want to follow the Atlas naming conventions but there is nothing about method overloading :-)

    For information I blog about this here : Surcharge de méthode (in French)

  • Re: Method Overloading with JavaScript

    11-19-2006, 8:34 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs

    hello again.

    ah, ok. i didn't read the whole post.

    regarding the code, i do like the idea. regarding the name convention, well, since it's your framework i think you're free to choose the names you want. 

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: Method Overloading with JavaScript

    11-22-2006, 5:53 PM
    • Star
      14,095 point Star
    • bleroy
    • Member since 04-12-2003, 3:09 AM
    • Redmond
    • Posts 2,296

    I do like the idea *if* there is a preprocessor that generates the main method's parameter counting goo from the raw overloads that I declare without any other consideration about JavaScript not supporting it out of the box.

    On the other hand, it may confuse users into thinking they doing something they're not, and the debugging experience may look weird. Furthermore, looking at the arguments collection is a *very* expensive operation.

    So in the end, I'm not convinced this is worth the additional complexity. From our experience, optional parameters and additional methods are OK as workarounds. It's just JavaScript, we don't have to map every single concept that exists in the managed world.

    Bertrand
    ----
    This posting is provided "AS IS" with no warranties, and confers no rights.
Page 1 of 1 (5 items)