Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JQuery is it possible to skip an optional argument and pass the next ones?

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.

like image 871
MTVS Avatar asked Aug 25 '12 21:08

MTVS


People also ask

How do you pass optional parameters while omitting some other optional parameters?

Use the parameter?: type syntax to make a parameter optional. Use the expression typeof(parameter) !== 'undefined' to check if the parameter has been initialized.

Can we pass optional parameter in JavaScript?

How Optional Parameters work? Usually, when you don't pass parameters, 'undefined' is passed instead. But using Optional Parameters, you can define a default value. So, whenever no value or undefined is passed, a default value is passed in its place.

How do I skip optional parameter TypeScript?

To omit one optional parameter, while providing another in a TypeScript function, pass an undefined value for the optional parameter you want to omit and provide the value for the next, e.g. logArguments(100, undefined, ['a', 'b', 'c']) .

How do you pass multiple optional parameters in TypeScript?

In Typescript, making optional parameters is done by appending the “?” at the end of the parameter name in the function when declaring the parameters and the parameters which are not marked with “?” i.e not optional parameter are called as default parameters or normal parameters where it is must and compulsory to pass ...


3 Answers

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:

.load(url [, data] [, complete(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.

like image 180
Salman A Avatar answered Sep 28 '22 08:09

Salman A


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?)

like image 22
Ry- Avatar answered Sep 28 '22 10:09

Ry-


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"
like image 32
David G Avatar answered Sep 28 '22 10:09

David G