JavaScript Arguments and No Overloading

By:    Updated: January 23,2017

JavaScript Arguments

Javascript function arguments don’t behave in the same way as function arguments in most other languages. An Javascript function doesn’t care how many arguments are passed in, nor does it care about the data types of those arguments. Just because you define a function to accept two arguments doesn’t mean you can pass in only two arguments. You could pass in one or more, and the interpreter won’t complain. This happens because javascript function arguments are represented as an array internally. The array is always passed to the function, but the function doesn’t care what (if anything) is in the array. In fact, there actually is an arguments object that can be accessed while inside a function to retrieve the values of each argument that was passed in.

<script type="text/javascript">
function valinv()
{
	//The arguments object acts like an array (though it isn't an instance of Array)
	var fs_length = arguments.length; //get arguments' amount
	var call_tips = "";

	if(fs_length > 0)
	{
		for(var i=0; i<fs_length; i++)
		{
			call_tips += "arguments[" + i + "]:" + arguments[i] + ";";
		}
		alert(call_tips);
	}
	else
	{
		alert("You donn't pass any arguments to this function~");
	}
}

valinv("hello everybody,");
valinv("hello everybody,", "welcome to valinv");
valinv("hello everybody,", "welcome to valinv", (new Date()).getDate());
</script>

The arguments object can be used in conjunction with named arguments, such as the following:

<script type="text/javascript">
function valinv(site_name, site_language)
{
	//The named argument site_name holds the same value as arguments[0],
	alert("arguments[0]:" + arguments[0] + "; site_name:" + site_name);

	//Any named argument that is not passed into the function is automatically assigned the value undefined.
	alert("site_language:" + site_language);
}

valinv("valinv");
</script>

JavaScript No Overloading

Javascript functions cannot be overloaded in the traditional sense. In other languages, such as Java and C++, it is possible to write two definitions of a function (they have the same function name) so long as their signatures (the type and number of arguments accepted) are different. As just covered, javascript functions don’t have signatures, because the arguments are represented as an array containing zero or more values. Without function signatures, true overloading is not possible.

<script type="text/javascript">
function valinv(site_name, site_language)
{
	alert("This function has to named arguments: site_name and site_language");
}
function valinv(site_language)
{
	alert("This function only has one named arguments: site_language");
}

//The last function will overwrite the function before it
valinv("valinv", "en");
</script>

The length of the arguments object is set based on the number of arguments passed in, not the number of named arguments listed for the function.So

<script type="text/javascript">
function valinv(site_name, site_language)
{
	arguments[1] = "en";
	alert("site_name:" + site_name + "; site_language:" + site_language);
}

//site_name:valinv; site_language:undifined
valinv("valinv");

//site_name:valinv; site_language:en
valinv("valinv", "other");
</script>
More in Development Center
New on Valinv
Related Articles
Sponsored Links