Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interpret documentation for optional JavaScript parameters [duplicate]

From the documentation for addEventListener I see the following pattern:

target.addEventListener(type, listener[, useCapture]);

Now I understand that useCapture is an optional parameter. Why does the [ then start before the comma (,) and not immediately after the comma that follows the listener parameter? What does the enclosing pair of [] actually suggest apart from the fact that useCapture is optional? I have also seen similar documentation patterns in jQuery documentation, e.g. the on () method documentation.

.on( events [, selector ] [, data ], handler(eventObject) )
like image 260
Geek Avatar asked Jun 16 '13 09:06

Geek


People also ask

How do you document optional parameters?

To indicate optional arguments, Square brackets are commonly used, and can also be used to group parameters that must be specified together. To indicate required arguments, Angled brackets are commonly used, following the same grouping conventions as square brackets.

Can you have multiple optional parameters in JavaScript?

Also, you can have multiple optional parameters, as shown in the following snippet, as long as you define them at the end of the parameter list. So as you can see, the JavaScript ES6 syntax is much simpler and easier to write than the old method.

Can a method have 2 optional parameters?

Can a method have 2 optional parameters? If you want to define multiple optional parameters, then the method declaration must be as shown below. The defined GetDetails() method can be accessed as shown below. GetDetails(1); GetDetails(1, “Rohini”);


1 Answers

Square brackets mean the thing inside them is optional – either you have it or you don't. It is a concise way of listing the valid invocation forms.

The Basic Example

target.addEventListener(type, listener[, useCapture]);

There are two valid forms:

target.addEventListener(type, listener            ); // without
target.addEventListener(type, listener, useCapture); // with

If the comma was outside the square brackets the two forms would be

target.addEventListener(type, listener,           ); // without (syntax error)
target.addEventListener(type, listener, useCapture); // with

The jQuery Example

.on( events [, selector ] [, data ], handler );

This one is a bit tricky. The selector and data are optional so there are four valid forms:

.on( events                , handler ); // without both
.on( events          , data, handler ); // without selector, with data
.on( events, selector      , handler ); // with selector, without data
.on( events, selector, data, handler ); // with both

The problem is that the second and third forms both have three parameters, so it's not obvious how the arguments will be interpreted. It appears that jQuery decides based on the type of the middle argument: if it's a string the third form is chosen; otherwise the second form is chosen.

So the following will have "hi" as the selector and nothing as the data argument:

.on( events          , "hi", handler ); // "hi" is the selector (!)

To force jQuery to use "hi" as the data argument, null must be given for the selector:

.on( events, null    , "hi", handler ); // "hi" is the data argument

This is unambiguously the fourth form, and the docs say that a null selector is treated the same an omitted selector.

A Nested Example

In documentation you'll often see nested square brackets. Here is a simplified example from the documentation for the Unix command man:

man [--warnings[=type]] page

This means the following forms are valid:

man                   javac    # without outer
man --warnings        javac    # with outer (without inner)
man --warnings=number javac    # with outer (with inner)

But the following would not be valid:

man           =number javac    # is this with or without outer?
like image 108
tom Avatar answered Oct 12 '22 00:10

tom