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.
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.
Share Improve this question edited Jan 10, 2013 at 12:05 Salman Arshad 272k84 gold badges443 silver badges534 bronze badges asked Aug 25, 2012 at 21:56 MTVSMTVS 2,0965 gold badges27 silver badges38 bronze badges 4- 2 Do you have an example of a method you want to do this on? – Christofer Eliasson Commented Aug 25, 2012 at 21:58
- Off topic, but i know that its always good to put optional parameter as the last parameter – insomiac Commented Aug 25, 2012 at 21:59
- jQuery.get( url [, data] [, success(data, textStatus, jqXHR)] [, dataType] ) $.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 – MTVS Commented Aug 25, 2012 at 22:05
- @csstd: jQuery should automatically determine which parameter it is from the type, but if it can't, that's when you use an object for options. – Ry- ♦ Commented Aug 25, 2012 at 22:10
3 Answers
Reset to default 7Yes 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
:
.load(url [, data] [, plete(responseText, textStatus, XMLHttpRequest)])
And the following work as expected:
.load("test.php")
.load("test.php", {foo: bar})
.load("test.php", {foo: bar}, function(){})
.load("test.php", function(){}) // you skipped the 2nd parameter? no.
You'll notice that the three parameters are: string
, object
and function
. 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 that data
parameter was skipped and act accordingly.
To your surprise, there is another jQuery.load
function with different signature which does something entirely different:
.load(handler(eventObject))
.load([eventData], handler(eventObject))
Again, jQuery can determine which method to fire by looking at the arguments.
Not as you might be used to. Some functions might take an object for options, such as $.ajax
:
$.ajax({
url: 'whatever',
data: {some: 'thing'}
});
As opposed to:
$.ajax('whatever', {
data: {some: 'thing'}
});
Using the former form would allow you to omit the URL and use the default. (Not the best example, but it's the only one I can think of out of all of jQuery offhand. Do you have a particular case in mind?)
This isn't a matter of jQuery, as it has more to do with the functionality of JavaScript itself. The jQuery functions were made utilizing arguments. Arguments that are passed the function have types. We can test what type these arguments are and therefore determine what they are being used for.
function foo( show, text ) {
if ( typeof show === "function" ) show( text );
else if ( !show ) alert( text );
}
We use typeof
to find the type. If an argument is excluded from the function call, its type is undefined
. undefined
is a falsy type. Other falsy types are 0
, false
, NaN
, empty string (""
), and null
. We can test a variables falsy-ness using the logical NOT operator, !
.
Moving on, if we know that show
is a function, we can use it as a function and call it with the argument text
.
foo(function (text) { alert(text); }, "Hello World"); // alerts "Hello World"