Click to See Complete Forum and Search --> : Overloaded functions? x(y) + x(y,z)


onerrorgoto
09-15-2003, 09:34 AM
Is it possible to make overloaded functions in javascript?

I have a function that alters a cell in a table,
now I would also want to be able to specify which row I want the cell to be alterd.

like this:

function changevalue(newvalue)
{
}
function changevalue(rowid,newvalue)
{
}


Is this possible in javascript?

Khalid Ali
09-15-2003, 09:52 AM
yes you can do that...the best thing in JavaScript about it that you don't have to define a function with the arguments

such as in your exampes
function changevalue(newvalue){}
function changevalue(rowid,newvalue){}


you could use this instead
function changevalue()//notice no parameters
and then
var args = changevalue.arguments;
where args is an array of parameters

hope this heps

onerrorgoto
09-16-2003, 12:55 AM
Thank you
The reason I asked is that I tried to do just as in my example, but I got an error when I called the my function with 2 inparam. The code jumped in to the function with 3 inparams and complained that the third param was undefined.
This is my two functions, maybe something is wrong in them.

Anyway, I will try to debug some more.

This is my 2 functions:



function lvwRefByStd_setCellValue(cIndex,NewText)
{
//get Id of selected row
var rowID=lvwRefByStd_getSelectedID();
//check if any row is selected
if(rowID=="")
{
alert("No row has been selected");
}
else
{
//create cellid from rowID + cIndex
var cellId=rowID + "_c" + cIndex;
//get the td-object
var iTd=document.getElementById(cellId);
//put new textin td-tag
iTd.innerHTML = NewText;
}
}

function lvwRefByStd_setCellValue(rIndex,cIndex,NewText)
{
//get Id of given row
var rowID="tbllvwRefByStd_r" + rIndex;
//check if any row is given
if(rowID=="")
{
alert("No row has been selected");
}
else
{
//biuld cellID from rowID + cIndex
var cellId=rowID + "_c" + cIndex;
//get td-object
var iTd=document.getElementById(cellId);
//put new text in td-tag
iTd.innerHTML = NewText;
}
}




Thanks
Anders

Xin
09-16-2003, 01:07 AM
you can't overload a function in javascript, by doing so, you will just overwrite the previous one

onerrorgoto
09-16-2003, 01:37 AM
But was is Khalid Ali talking about then?
yes you can do that...the best thing in JavaScript about it that you don't have to define a function with the arguments

/Anders