I found this in a jQuery file:
xxx.css({ 'float' : 'right' });
What do the curly braces do?
I found this in a jQuery file:
xxx.css({ 'float' : 'right' });
What do the curly braces do?
In your case it is an object passed to your css function.
myObj={} // a blank object
Here you can use this too
myObj={'float' : 'right'}
xxx.css(myObj);
Here is another example of object
var myObj={
'varOne':'One',
'methodOne':function(){ alert('methodOne has been called!')}
}
myObj.methodOne(); // It will alert 'methodOne has been called!'
A fiddle is here.
The curly braces in the code you've shown define an object literal
This is the top search engine result for "javascript braces". As such it's worth mentioning that braces in JavaScript can be used for:
In javascript curly braces are used for several purposes.
I your case these are used to create a key-value pair.
In others cases curly braces are used to combine a set of statements in a block. And sometimes they are used to create objects like var abc = { "a": 1, "b": 2 };
It's an object literal.
var x = {'float': 'right'}
is the nicer/shorter form of var x = new Object(); x.float = 'right';
Basically the curly braces {} are the another way for creating objects in javascript. This is equivalent to the "new Object()" syntax.
curly braces identify an Object like so:
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 an Object with options for your method.
You could also write your code like so xxx.css("width","10px").css("font-size","30px");
But passing it an Object makes it faster and more readable
xxx.css({"width":"10px","font-size":"20px"});
Creates an object.
var myObject = {"element" : "value"};
alert(myObject.element); // Would alert: "value"
That is an object literal
An object literal is a list of zero or more pairs of property names and associated values of an object
They encapsualte the css attributes in this example.
Normally curly brackets represent a function or an encapsulated piece of code that needs to be executed as one.