Click to See Complete Forum and Search --> : javascript funtions... overloading?


Jessica Nieves
August 26th, 2002, 05:34 PM
Hi everyone!
I want to know if in JavaScript I can have two functions with same names, but different amount of arguments. For example one function fistFunction(arg1) and another fistFunction(arg1, arg2, arg3) this is called function overloading in C, but Im not sure if I can use this in JavaScript.

Thanx :D

websmith99
August 26th, 2002, 06:59 PM
I am almost positive that this is not allowed due to the following:

- JavaScript will allow you to pass any number of arguments to a function, despite how many are defined in the constructor. If you pass more arguments than the function expects, the extra values are simply ignored. If you pass fewer than expected, some of the parameters are given the undefined value.

Let's say that you have the function call

fistFunction('foo', 'bar')

By the above logic, which function would be called, fistFunction(arg1) or fistFunction(arg1, arg2, arg3) ? Because of this ambiguity I am pretty sure JavaScript would not allow function overloading.

Of course, this is my theory- :D

Waldo2k2
August 26th, 2002, 08:11 PM
you can't do function overloading like that of C or C++ in JavaScript, simply because you don't need to as you do in C or C++. You can pass it as many variables as you want, and since it decides what kind of variable it is itself, you can basically send it any kind of whacky arguments you feel like. Like Websmith said extraneous arguments are just ignored, so

function someFunction( var var1, var var2, var var3)
{
//whatever
}


<body>
<a href="#" onclick="someFunction(1,'sports','cat',3)">some link</a>
</body>

In the above example the fourth variable passed (happens to be the number 3 here) would just be ignored.

Zvona
August 27th, 2002, 01:00 PM
Every passed parameter is stored in arguments[] array and can be exctracted very easily. I usually use it. Here's an simple example of zImage(sSrc) function, where only source has to be defined :

function zImage(sSrc)
{
var oTempImg = new Image();
oTempImg.src = sSrc;
if (arguments[1])
oTempImg.width = arguments[1];
if (arguments[2])
oTempImg.height = arguments[2];

oTempiImg.alt = (arguments[3])?arguments[3]:"";

return oTempImg;
}

And :
var myImg1 = new zImage('theImage.gif');
- creates an image object with source 'theImage.gif', with default width and height. Alt = ""

var myImg2 = new zImage('theImage.gif',100,100);
- creates an image object with source 'theImage.gif' with width=100 and height=100. Alt = ""

var myImg3 = new zImage('theImage.gif',false,200,'An image');
- creates an image object with source 'theImage.gif', with default width and height=200. Alt = 'An image'.

Note that order of the parameters should be thinked very wisely. In above example, alt could be before width and height, because it's required.

websmith99
August 27th, 2002, 02:31 PM
Going a step further, you can determine the number of arguments passed to a function and the number of arguments the function expects to be passed:

function foo(arg1, arg2, arg3) {
// the number of arguments expected
var expected = arguments.caller.callee.length;
// the number of arguments actually passed
var passed = arguments.caller.length; // or arguments.length
// rest of code here ...
}


Now does anyone have any ideas on my Netscape 4.7x on Solaris bug? (posted 08/22/2002 12:02am)

Waldo2k2
August 27th, 2002, 09:44 PM
sorry, i know nothing about solaris so i wouldn't know why the bug would be occuring.