tags:

views:

2744

answers:

8

I'm learning javascript and while browsing throught the JQuery library I see ':' being used alot. What is this used for in javascript?

// Return an array of filtered elements (r)
// and the modified expression string (t)
   return { r: r, t: t };
+28  A: 
var o = {
    r: 'some value',
    t: 'some other value'
};

is functionally equivalent to

var o = new Object();
o.r = 'some value';
o.t = 'some other value';
Justice
So similar to C# object initializer syntax. Thanks!
Micah
A: 

That's JSON, or JavaScript Object Notation. It's a quick way of describing an object, or a hash map. The thing before the colon is the property name, and the thing after the colon is its value. So in this example, there's a property "r", whose value is whatever's in the variable r. Same for t.

JW
JSON is only a subset of JavaScript object initialization syntax. '{ a: k() }' where k is a function is not JSON, but it is perfectly fine JavaScript object initialization syntax.
Justice
To be pedantic, no, it's not "JSON". It _looks_ like JSON. It's the object literal syntax that is native to JavaScript and that can appear directly inside code. JSON on the other hand is a data serialization/interchange format. JSON is JSON only when it's "airborne", i.e. in transit or when it's not yet parsed into a real object.
Ates Goral
+1 for Ates Goral, but note that the example given doesn't even *look* like JSON: the names would have to be in double-quotes for it to be valid JSON syntax.
NickFitz
+14  A: 
Jonathan Lonowski
The only thing wrong with that diagram is that it says "string" instead of "name" - I realize that real JSON requires a string but it's not an accurate depiction of a real JavaScript object.
J-P
Actually, real JavaScript objects strictly use strings as property names. If you use a non-string variable as a property name, it will be cast to a string. This is true even for arrays: var a = [ 42 ]; for (var i in a) alert(typeof i); // will give you "string"
Ates Goral
+3  A: 

It is part of the object literal syntax. The basic format is:

var obj = { field_name: "field value", other_field: 42 };

Then you can access these values with:

obj.field_name; // -> "field value"
obj["field_name"]; // -> "field value"

You can even have functions as values, basically giving you the methods of the object:

obj['func'] = function(a) { return 5 + a;};
obj.func(4);  // -> 9
bandi
+4  A: 

The ':' is a delimiter for key value pairs basically. In your example it is a Javascript Object notation.

In javascript, Objects are defined with the colon delimiting the identifier for the property, and its value so you can have the following:

return { 
    Property1 : 125,
    Property2 : "something",
    Method1 : function() { /* do nothing */ },
    array: [5, 3, 6, 7]
};

and then use it like:

var o =  { 
    property1 : 125,
    property2 : "something",
    method1 : function() { /* do nothing */ },
    array: [5, 3, 6, 7]
};

alert(o.property1); // Will display "125"

It is also known as JSON (Javascript Object Notation) which is useful in AJAX calls because it is compact and quick to parse in server-side languages and Javascript can easily de-serialize a JSON string into an object like:

// The parenthesis '(' & ')' around the object are important here
var o = eval('(' + "{key: \"value\"}" + ')');

You can also put the key inside quotes if it contains some sort of special character or spaces, but I wouldn't recommend that because it just makes things harder to work with.

Dan Herbert
+8  A: 

You guys are forgetting that the colon is also used in the ternary operator (though I don't know if jquery uses it for this purpose).

the ternary operator is an expression form (expressions return a value) of an if/then statement. it's used like this:

var result = (condition) ? (value1) : (value2) ;

A ternary operator could also be used to produce side effects just like if/then, but this is profoundly bad practice.

Breton
you mean A ternary operator?
micmoo
AKA "ternary operator". Note that the OP is strictly asking about the object literal case. If we're to go even beyond what the OP is asking, the colon is also used in labels.
Ates Goral
yes I did mean that. I should just stay off the internet, really, if i'm going to go around flagrantly mis-identifying programming concepts like that.
Breton
+6  A: 

And also, a colon can be used to label a statement. for example

var i = 100, j = 100;
outerloop:
while(i>0) {
  while(j>0) {
   j++

   if(j>50) {
     break outerloop;
   }
  }
i++

}
Breton
Oh noes! A GOTO in disguise!!! :)
Ates Goral
+1  A: 

Just thought I'd mention that another use of the colon is to assign data types to variables -

var s : String;

means that the variable s is of type String

anotheranswer