Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k 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 Reply

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...