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. how can i pass this counterstart
variable to me function?
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.
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 ];
}
}
精彩评论