What do curly braces mean in JavaScript?

I found this in jQuery file:

xxx.css({ 'float' : 'right' }); 

What do curly braces do?

+44
javascript jquery css
Mar 14 '12 at 9:25
source share
10 answers

In your case, this is an object passed to your css function.

 myObj={} // a blank object 

You can also use this here.

 myObj={'float' : 'right'} xxx.css(myObj); 

Here is another example of an object

 var myObj={ 'varOne':'One', 'methodOne':function(){ alert('methodOne has been called!')} } myObj.methodOne();​ // It will alert 'methodOne has been called!' 

The violin is here .

+47
Mar 14 '12 at 9:29
source share

Curly braces in the code you specify define the object literal

+12
Mar 14 '12 at 9:28
source share

This is an object literal.

var x = {'float': 'right'} is a more beautiful / shorter form var x = new Object(); x.float = 'right'; var x = new Object(); x.float = 'right';

+5
Mar 14 '12 at 9:28
source share

Creates an object.

 var myObject = {"element" : "value"}; alert(myObject.element); // Would alert: "value" 
+4
Mar 14 '12 at 9:28
source share

This is an object literal

A literal object is a list of zeros or more pairs of property names and associated object values

+4
Mar 14 '12 at 9:28
source share

In javascript, braces are used for several purposes.

In your case, they are used to create a key-value pair.

In other cases, braces are used to combine a set of instructions in a block. And sometimes they are used to create objects like var abc = {"a": 1, "b": 2};

+4
Mar 14 2018-12-12T00:
source share

braces identify the object as follows:

 timObject = { property1 : "Hello", property2 : "MmmMMm", property3 : ["mmm", 2, 3, 6, "kkk"], method1 : function(){alert("Method had been called" + this.property1)} }; 

in jQuery, they are used to provide the object with options for your method. You can also write your code like this xxx.css("width","10px").css("font-size","30px"); But by passing it, Object makes it faster and more readable.

 xxx.css({"width":"10px","font-size":"20px"}); 
+3
Mar 14 '12 at 9:27
source share

Basically, curly braces {} is another way to create objects in javascript. This is equivalent to the syntax "new object ()".

+2
Mar 14 2018-12-12T00:
source share

Creates an object literal.

More if you want: http://www.dyn-web.com/tutorials/obj_lit.php

+1
Mar 14 '12 at 9:29
source share

They complete the css attributes in this example.

Typically, braces represent a function or the encapsulated part of the code that must be executed as one.

0
Mar 14 '12 at 9:29
source share



All Articles