简体   繁体   中英

Square brackets and space in function declaration arguments Javascript

In Javascript what do the square brackets mean for declaring a function. And how can you declare more arguments with a space? Is this psuedocode?

router.METHOD(path, [callback, ...] callback)

Source: https://expressjs.com/en/4x/api.html#router.METHOD

If this should be a comment, let me know.

In this particular instance, it's saying that you can pass in any number of middleware functions, separated by commas, *AND/OR* one single callback to be run as the last part of the method.

The square brackets sort of represent an "array" (though not really an array) of however many middleware functions you want to run before the final function call for the method.

The space after the square brackets is sort of an *AND/OR* character saying that you can pass however many middleware functions, AND a final function, OR just the final function without the middleware functions.

They're not declaring a function here, in the documentation. They're showing you "this is how many functions you can pass as middleware for this method which has been defined elsewhere: as many as you want, AND THEN the final function, or zero, AND THEN the final function"

Clear as mud?

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