Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-11-2006, 10:31 AM   PM User | #1
jak0b
New Coder

 
Join Date: Apr 2006
Location: Poland
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
jak0b is an unknown quantity at this point
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
jak0b is offline   Reply With Quote
Old 04-11-2006, 11:15 AM   PM User | #2
Kakao
Regular Coder

 
Join Date: Mar 2006
Location: Brasília, Brazil
Posts: 153
Thanks: 0
Thanked 0 Times in 0 Posts
Kakao is on a distinguished road
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
Kakao is offline   Reply With Quote
Old 04-11-2006, 02:53 PM   PM User | #3
SpirtOfGrandeur
Regular Coder

 
Join Date: May 2005
Location: Michigan, USA
Posts: 566
Thanks: 0
Thanked 0 Times in 0 Posts
SpirtOfGrandeur is an unknown quantity at this point
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];
}
__________________
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.

Last edited by SpirtOfGrandeur; 04-11-2006 at 03:07 PM..
SpirtOfGrandeur is offline   Reply With Quote
Old 04-11-2006, 03:11 PM   PM User | #4
Beagle
Senior Coder

 
Join Date: Jul 2005
Location: New York, NY
Posts: 1,084
Thanks: 4
Thanked 19 Times in 19 Posts
Beagle is an unknown quantity at this point
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";
  }
}
Beagle is offline   Reply With Quote
Old 04-11-2006, 03:37 PM   PM User | #5
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 04-11-2006, 03:58 PM   PM User | #6
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 04-11-2006 at 04:01 PM..
Kor is offline   Reply With Quote
Old 04-11-2006, 04:31 PM   PM User | #7
Beagle
Senior Coder

 
Join Date: Jul 2005
Location: New York, NY
Posts: 1,084
Thanks: 4
Thanked 19 Times in 19 Posts
Beagle is an unknown quantity at this point
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.
Beagle is offline   Reply With Quote
Old 04-11-2006, 04:45 PM   PM User | #8
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 04-11-2006, 04:57 PM   PM User | #9
Beagle
Senior Coder

 
Join Date: Jul 2005
Location: New York, NY
Posts: 1,084
Thanks: 4
Thanked 19 Times in 19 Posts
Beagle is an unknown quantity at this point
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.
Beagle is offline   Reply With Quote
Old 04-11-2006, 05:01 PM   PM User | #10
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 04-11-2006, 05:13 PM   PM User | #11
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
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
brothercake is offline   Reply With Quote
Old 04-11-2006, 05:14 PM   PM User | #12
SpirtOfGrandeur
Regular Coder

 
Join Date: May 2005
Location: Michigan, USA
Posts: 566
Thanks: 0
Thanked 0 Times in 0 Posts
SpirtOfGrandeur is an unknown quantity at this point
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.
SpirtOfGrandeur is offline   Reply With Quote
Old 04-11-2006, 05:18 PM   PM User | #13
Beagle
Senior Coder

 
Join Date: Jul 2005
Location: New York, NY
Posts: 1,084
Thanks: 4
Thanked 19 Times in 19 Posts
Beagle is an unknown quantity at this point
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.
Beagle is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:26 AM.


Advertisement
Log in to turn off these ads.