开发者

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 ];
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜