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..
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";
}
}
<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>
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.
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.
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?
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.
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
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.
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.