简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM