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';
ID : 20060
viewed : 2
Tags : javascriptjavascript
98
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';
82
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++ }
75
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.
65
The ':' is a delimiter for key value pairs basically. In your example it is a Javascript Object Literal 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"
A subset of this 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.
// 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.
Keep in mind that JavaScript Object Literal Notation in the JavaScript language is different from the JSON standard for message passing. The main difference between the 2 is that functions and constructors are not part of the JSON standard, but are allowed in JS object literals.
51
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