I have question on how to read node js documentation. I often see these kinds of argument notation below:
- http.createServer([options][, requestListener])
*arguments in square bracket ie. [options]
*square bracket, comma, and argument name ie. [, requestListener]
- emitter.emit(eventName[, ...args])
* [,...args]?
- http.get(options[, callback])
* arguments with no bracket ie. options
what do these argument notation mean?
I might not entirely correct, but across numerous documentations that I have come across. The above is a more a loosely created notation to explain the arguments formats.
[ ... ] is normally associated with OPTIONAL
The comma placed inside the square brackets is similar to the regex notation as such
meaning the regex is match
meaning if you are going to put in a 2nd argument, it should be separated from the first using a comma.
Hence
Code:
http.createServer([options][, requestListener])
Would means you can invoke the createServer method using the following possible arguments
Code:
http.createServer();
http.createServer({ .... });
http.createServer({ .... }, function() { ... });
That being said, the documentation is loosely presented for example.
The above method syntax does not in anyway denote that the first OBJECT "options" can be optionally left out and immediately just specify the 2nd argument, in which the syntax should allow the comma(,) be placed immediately in front of the function argument as such
Code:
http.createServer(, function() { ... });
Obviously, if you try doing this, the javascript parser will choke. So this is more of common sense instead of a formal syntax declaration.
Hence you can write either of these for the above case
Code:
http.createServer(function() { ... })
http.createServer(() => { ... })
This is why I say this is a very loosely written documentation
A more formal way of writing the above intention should be the following
Code:
http.createServer([options | requestListener])
http.createServer(options, requestListener)
Code:
emitter.emit(eventName[, ...args])
The above, it should be interpreted as eventName is mandatory, and there could be zero or more arguments the follow, as such.
Code:
emitter.emit(eventName)
emitter.emit(eventName, arg1)
emitter.emit(eventName, arg1, arg2, ..., argN)
The last I will leave to you interpret it for yourself