[英]Passing an argument to a function in Javascript
我有一個按鈕,每次單擊時都會添加一個段落。 在我的代碼中,我有一個名為addPara(count)
的函數,並且我有一個counterstart
變量。 我如何將此counterstart
變量傳遞給我的功能?
addPara(counterstart);
如果您不確定不確定要傳遞多少個參數,則可以通過函數內部的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.