Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Apr 2006
    Location
    Poland
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question multi constructors in oop js

    all object oriented langs give a posibility to implement many construstors

    can I have few constructors in js
    i.e.:
    Code:
    function X()
    {
    	this.Id = 1;
    }
    function X(id)
    {
    	this.Id = id;
    }
    X.prototype.print = function()
    {
    	alert( 'Id: '+this.Id );
    }
    var x1 = new X();
    var x2 = new X(5);
    it doesn't work, print return:
    for x1: Id: undefined
    for x2: Id: 5

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I know your question is if different signatures (name + arguments) make different constructors. I can't answer that but I know this works:

    PHP Code:
    function (id) {
      if (
    arguments.length == 0this.id 1;
      else 
    this.id id;
      }
    x1 = new X();
    alert(x1.id);
    x2 = new X(5);
    alert(x2.id); 





    Programming Tutorial for absolute beginners

  • #3
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Kakao has the correct idea. And you can even go as far as to list as many as you want. It is all stored in the arguments array.

    Code:
    function X() {
        if (arguments.length == 0) this.id = 1;
        else this.id = arguments[0];
    }
    Last edited by SpirtOfGrandeur; 04-11-2006 at 03:07 PM.
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.

  • #4
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    What you're talking about is method overloading. JavaScript does not support method overloading. PHP has objects, it does not allow method overloading either. Your claim that all OOPLs support multiple constructors is simply wrong.

    In order to overload a function in JavaScript, you need to detect the number of arguments sent to the function, as in the above posts, and in the following example:

    Code:
    function jump1Arg(p_arg1)
    {}
    
    function jump2Arg(p_arg1, p_arg2)
    {}
    
    function jump3Arg(p_arg1, p_arg2, p_arg3)
    {}
    
    function jump()
    {
      switch (arguments.length)
      {
         case 1:
           jump1Arg(arguments[0]);
           break;
        case 2:
          jump2Arg(arguments[0], arguments[1]);
          break;
        case 3:
          jump3Arg(arguments[0], arguments[1], arguments[2]);
          break;
        default:
          throw "Incorrect number of arguments ("+arguments.length+") to jump";
      }
    }

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I would have used an object:
    PHP Code:
    <script type="text/javascript">
    function 
    X(obj,arg){
    obj=arg?{id:arg}:{id:1};
    return 
    obj
    }
    onload=function(){
    var 
    x1 = new X(this);
    var 
    x2 = new X(this,5);
    alert(x1.id+'|'+x2.id)
    }
    </script> 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Beagle
    you need to detect the number of arguments sent to the function
    I don't really think so. If needed, you may test the existence of an argument, but there is no need to detect the number of the arguments (see my example above). Only when you pass the argument as an array, you need to loop throught the array, but even then there is no need to detect the length of the array as a value, just use the lenght attribute.
    Last edited by Kor; 04-11-2006 at 04:01 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    I see your point, you don't NEED to detect the length of the array, you can check for the existence of an argument, but if you intend to write complex functions that require you to break out the code into sub-function as I did with jump, then using a switch on arguments.length is a clean way to do it. Depending on the number of overloads, a switch may be faster than a bunch of if-elseifs.

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Beagle
    I see your point, you don't NEED to detect the length of the array, you can check for the existence of an argument, but if you intend to write complex functions that require you to break out the code into sub-function as I did with jump, then using a switch on arguments.length is a clean way to do it. Depending on the number of overloads, a switch may be faster than a bunch of if-elseifs.
    Yes, a switch is faster, it might be done with a switch instead of an if/for. But why using a bunch of functions? And an extra function to send the arguments to the proper function? Isn't it simplier to use that switch inside an unique function?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    really it depends on the complexity of the problem.

    If all the extra arguments do are provide overriding values to member variables, and if you omit them you use defaults, then clearly it doesn't make sense to do what I did.

    However, if your constructor can take a different number of dom elements, and based on the type / number of elements passed in, create members and wire up extra methods, etc, then separate functions might be called to make your code more readable / maintainable.

    Of course, use the right tool for the job. My example shouldn't be used just for overriding default values.

  • #10
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    well, probably the safest and pedantic way to code could be

    function X(argArray){

    }

    var x1 = new X([1,null,null])
    var x2 = new X([1,null,3])
    var x3 = new X([null,2,3])

    or something like that... or using false instead of null. Yes, it depends on the specific conditions
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    just a brief pedantic aside - arguments is not an array, it's a collection.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #12
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor
    well, probably the safest and pedantic way to code could be

    function X(argArray){

    }

    var x1 = new X([1,null,null])
    var x2 = new X([1,null,3])
    var x3 = new X([null,2,3])

    or something like that... or using false instead of null. Yes, it depends on the specific conditions
    Kor why are you creating an array when it is already an array based element. You are creating a 2-D array for no reason.
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.

  • #13
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    right, instead you could be doing:

    Code:
    function X(arg1, arg2, arg3, arg4)
    {}
    
    var x1 = new X(null, null, null, "jobs");
    var x2 = new X(5, 6, true, null);
    And the reason NOT to do that is because the OP wants to simulate overloading. Granted, the "non-pendantic" answers aren't 100% safe, but it gets the OP what he wanted.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •