Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

express - 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

question from:https://stackoverflow.com/questions/65909906/square-brackets-and-space-in-function-declaration-arguments-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...