I am a little bit confused by the jQuery documentation. I am looking at this page describing $.getJSON. The code sample is:
$.getJSON('ajax/test.json', function(data) {
var items = [];
$.each(data, function(key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('body');
});
But the method signature is jQuery.getJSON( url [, data ] [, success(data, textStatus, jqXHR) ] ), where data is an object sent to the server, and success is a method called when the JSON request returns successfully.
So why does the example code work? It seems to have skipped the second argument. I would have expected the correct code to be:
$.getJSON('ajax/test.json', {}, function(data) {
// and then the same from here
I know that the square brackets mean that the [, data] and [, success] arguments are optional, but I guess I don’t understand how javascript deals with a variable number of arguments.
Thank you for your time.
Inside the jQuery source code :
So it works