[英]Passing an argument to a function in Javascript
I have a button which adds a paragraph each time it is clicked. 我有一个按钮,每次单击时都会添加一个段落。 In my code i have a function called
addPara(count)
, and i have a counterstart
variable. 在我的代码中,我有一个名为
addPara(count)
的函数,并且我有一个counterstart
变量。 how can i pass this counterstart
variable to me function? 我如何将此
counterstart
变量传递给我的功能?
addPara(counterstart);
If you mean that you're not sure how many arguments will be passed, you can reference any and all argument via the arguments
collection inside the function. 如果您不确定不确定要传递多少个参数,则可以通过函数内部的
arguments
集合引用任何和所有参数。
function addPara() {
console.log(arguments); // "arguments" is a collection of the arguments.
if(arguments[0] === undefined) {
// do something and return if no arguments were given
return false;
}
// otherwise loop through the arguments passed
for( var i = 0, len = arguments.length; i < len; i++ ) {
// do something with arguments[ i ];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.