In JQuery is it possible to skip an optional argument and pass the next ones? if yes, how a jquery function determines which value is for which parameter?
An example:
//
// function signature is
// jQuery.get( url [, data] [, success(data, textStatus, jqXHR)] [, dataType] )
//
// function is called as
//
$.get('ajax/test.html', function (data) {
$('.result').html(data);
alert('Load was performed.');
});
In call to $.get() after the url the [, data] is skipped and next the success callback is passed.
Yes it is possible to skip optional arguments, even from the middle. jQuery looks at the type of arguments to determine which is which. Have a look at the signature of
jQuery.load:And the following work as expected:
You’ll notice that the three parameters are:
string,objectandfunction. Inside the function jQuery can easily check how many arguments were passed and their types. If it notices that the 2nd parameter is a function it will assume thatdataparameter was skipped and act accordingly.To your surprise, there is another
jQuery.loadfunction with different signature which does something entirely different:Again, jQuery can determine which method to fire by looking at the arguments.