Why are we using ({ })
?
Is it delegate?
What does it mean to use this syntax?
What are we wrapping with it?
For example:
$.ajaxSetup ({ // <-- THIS error: fError, compelete: fComp, success: fSucc }); // <-- AND THIS
The this Keyword is a reference to DOM elements of invocation. We can call all DOM methods on it. $() is a jQuery constructor and in $(this), we are just passing this as a parameter so that we can use the jQuery function and methods.
The purpose of jQuery is to make it much easier to use JavaScript on your website. jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.
$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.
jQuery simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is easier to use compared to JavaScript and its other JavaScript libraries. You need to write fewer lines of code while using jQuery, in comparison with JavaScript.
I mean, what we are wrapping it ?
No. That's JavaScript object notation (JSON). In your example you're invoking the function ajaxSetup
with an object whose properties are:
error: fError, compelete: fComp, success: fSucc
For instance, to create an "user" object you could write:
user = { "name":"Oscar", "lastName":"Reyes" };
And then use one of its attributes:
alert( a.name );
Shows: Oscar
What you see there (in your code) is the creation of an object and passing it as an argument.
It would be equivalent to:
var setUpInfo = { "error": fError, "compelete": fComp, "success": fSucc }; $.ajaxSetup( setUpInfo );
{}
is object notation in JavaScript. For example:
$('selector').plugin({ option1: 'value' });
In this case you're passing an object containing your settings to the plugin. The plugin can deal with this as a object, whatever it's referenced as, for example:
settings.option1 //the option you passed in.
Of course it has a lot more uses, but this is the most common example in jQuery. The same is true for the .animate()
, $.ajax()
, .css()
functions, etc. Anything that takes properties generally uses this format.
As requested, some other examples:
Any object inside the passed object can be a function as well, not only properties, for example:
$("<input>", { type: "text", focusin: function() { alert("Hi, you focused me!"); } });
This would set the focus event of that input to have an alert. Another is extending an object, adding properties to it, like this:
var person = { first_name: "John" }; $.extend(person, { last_name: "Smith" }); //equivalent to: person.last_name = "Smith"; //or: person["last_name"] = "Smith";
Now person
has the last_name
property. This is often used by plugins as well, to take the default settings, then merge any settings you passed in, overwriting with any settings you specified, using defaults for the rest.
Why are we using it? Well...that's how JavaScript works, and in the jQuery spirit: it's an extremely terse and flexible way to pass information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With