Why do we use "({})" in jQuery?

Why are we using ({ }) ?

Is this delegation?

What does it mean to use this syntax?

What do we wrap with it?

For example:

 $.ajaxSetup ({ // <-- THIS error: fError, compelete: fComp, success: fSucc }); // <-- AND THIS 
+76
json javascript jquery
Apr 7 '10 at 18:00
source share
5 answers

{} is an object designation in JavaScript. For example:

 $('selector').plugin({ option1: 'value' }); 

In this case, you pass the object containing your settings to the plugin. A plugin can consider this as an object, regardless of what it refers to, for example:

 settings.option1 //the option you passed in. 

Of course, it has many more uses, but this is the most common example in jQuery. The same is true for .animate() , $.ajax() , .css() , etc. Anything that accepts properties usually uses this format.




As requested, some other examples:
Any object inside the transferred object can also be a function, and not just properties, for example:

 $("<input>", { type: "text", focusin: function() { alert("Hi, you focused me!"); } }); 

This would set the focal event of this input to alert. Another is the extension of an object, adding properties to it, for example:

 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 also often used by plugins to use the default settings, then combine all the parameters that you passed, overwrite with any parameters that you specified, using the default values ​​for the rest.

Why do we use it? Well ... the way javascript works, and in jQuery style: this is an extremely accurate and flexible way to convey information.

+156
Apr 07 2018-10-18T00:
source share

I mean, what are we wrapping it in?

No. This is a JavaScript object notation (JSON). In your example, you call the ajaxSetup function with an object whose properties are:

 error: fError, compelete: fComp, success: fSucc 

For example, to create a β€œcustom” object that 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 creating an object and passing it as an argument.

This will be equivalent to:

 var setUpInfo = { "error": fError, "compelete": fComp, "success": fSucc }; $.ajaxSetup( setUpInfo ); 
+10
Apr 07 '10 at 18:10
source share

This is either a literal of a JavaScript object , or more specifically, JSON when it comes to sending parameters via Ajax. JSON is a subset of JavaScript object literals.

For example:

 // This is JSON data sent via the Ajax request (JSON is subset of JavaScript object literals) var json = {id: 1, first_name: "John", last_name: "Smith"}; // This is a JavaScript object literal, it is not used for transfer of data so doesn't need to be JSON var jsol = {type: 'POST', url: url, data: json}; $.ajax(jsol); 

Read more about this here:

+7
Apr 7 2018-10-18T00:
source share

The question was about the notation "({})".

In this context, the parentheses "(...)" following the expression, such as $ .ajaxSetup, call the function indicated by the expression that is being called.

An expression inside parentheses (which can be a list of expressions separated by commas) results in a value (or list of values), which is the argument (s) passed to the function.

Finally, when "{...}" is used in the context of an expression, it creates an object with the specified name properties. This is similar to JSON, but it is, in general, any legitimate JS literal object.

+6
Apr 7 2018-10-21T00:
source share

If you mean in this context:

 $("#theId").click( function() { /* body here */ } ); 

Then ( function() {}) is an anonymous function. But without an example, you cannot be sure what you mean.

+4
Apr 07 '10 at 18:06
source share



All Articles