Forum Moderators: open

Message Too Old, No Replies

Function overloading?

         

benj0323

3:00 am on Oct 10, 2004 (gmt 0)

10+ Year Member



How exactly do you perform function overloading in javascript?

I read somehwere that all of the parameters of a function are stored in an array called arguments[]. Is this true?

Also, how do you count the number of elements in a javascript array? count(arguments[])?

Thanks for your help.

Rambo Tribble

5:12 am on Oct 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are looking for Java or C++ style overloading, where the nature of the parameters passed alter the method's properties, JavaScript doesn't have it.

You could, however, pass variables, determine their nature and branch your code.

Yes, the arguments passed to a function are stored in an array called arguments. Length is the property that exposes the number of elements in an array, as in:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function myFunc(a,b,c,d){
document.write('<p>Length of array: '+ myFunc.arguments.length +'<br \/>');
document.write('First array entry: '+ myFunc.arguments[0] +'<br \/><\/p>');
document.write('<p><h2>Array Contents:<\/h2>');
var ar_ln=myFunc.arguments.length;
for(var i=0;i<ar_ln;i++){
document.write(myFunc.arguments[i]+'<br \/>');
}
document.write('<\/p>');
document.close();
}
</script>
</head>
<body onload="myFunc(1,2,3);">
</body>
</html>