Ryan
Ryan

Reputation: 15

why does jquery ajax syntax use commas inside the bracket of each argument?

why is the jquery documentation syntax structured like this

jquery documentation = jQuery.post( url [, data ] [, success ] [, dataType ] )

instead of like this?

jQuery.post( url, [data], [success], [datatype])

the comma isn't going immediately after each parameter instead theres a bracket then the comma and I dont understand why its not structured like the second version above. any assistance appreciated.

Upvotes: 0

Views: 193

Answers (4)

Michael Sacket
Michael Sacket

Reputation: 907

Because what you're looking at is usage documentation, not actual JS syntax. The convention in most programming documentation is to use surrounding square brackets to denote optional parameters. If you remove the parameter, you'll want to remove the comma too, so it's included inside the bracket.

Upvotes: 4

Amnon
Amnon

Reputation: 2888

It's derived from an extension to the Backus-Naur form - the idea is that what is optional is inside the square brackets. In your second case, if for example data, success and datatype would not be used then that would imply the correct syntax for that would be:

jQuery.post( url, , , )

which of course is not what you want...

Upvotes: 0

random_user_name
random_user_name

Reputation: 26160

Brackets represent an optional parameter. That parameter does not have to be provided. And, if you do not provide that parameter, you should not include the comma.

It's even more clear when you consider the fact that - given the example you provided:

jQuery.post( url [, data ] [, success ] [, dataType ] )

You could omit only the data argument, and still pass in success and datatype, in which case jQuery automagically adjusts so that the signature seems like it is like so:

jQuery.post( url [, success ] [, dataType ] )

Additionally, it's not just the ajax documentation - it's many other functions as well.

For example, animate follows the same pattern:

.animate( properties [, duration ] [, easing ] [, complete ] )

And, once again, jQuery would automatically adjust if you only passed in some of them:

.animate( properties [, easing ] )

Upvotes: 0

The [] here means that the argument is optional, and the comma is only needed when this argument is used, so the comma is also optional.

Upvotes: 1

Related Questions