Home >Web Front-end >JS Tutorial >Examples of functions with variable number of parameters in JavaScript

Examples of functions with variable number of parameters in JavaScript

PHPz
PHPzOriginal
2016-05-16 16:34:341566browse

JavaScript allows a function to pass a variable number of parameters because there is a built-in object called arguments, which is an array of all parameters passed by a function.

Off-topic: I came into contact with JavaScript very early, but I didn’t pay attention to it. I saw many cool and dazzling web pages with JavaScript in them. Google’s application of JavaScript had the greatest impact on me. I was determined to learn it from scratch, hence the JavaScript & Ajax section. I plan to record this column as study notes, so the notes for each article may be very short, just one or two sentences of annotations.

JavaScript allows a function to pass a variable number of parameters because there is a built-in object called arguments, which is an array of all parameters passed by a function. Give an example and you will understand.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JavaScript参数个数可变的函数</title>
<mce:script language="javascript" type="text/javascript"><!--
function testparams()
{
var params = "";
for (var i=0; i<arguments.length; i++) {
params = params + " " + arguments[i];
}
alert(params);
}

testparams("abc", 123);
testparams(123, "456", 789);
testparams();
// --></mce:script>
</head>
<body>
</body>
</html>

The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn