tags:

views:

166

answers:

4

Take the example:

$.ajax({lhs:val});

What does the {} do? As far as I know, there's no named parameters -- so is this an actual member (same as $.ajax.lhs)? What does it mean and what does it do?

+4  A: 

That is an object literal (better know as a JSON object):

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

Andrew Hare
Yes, but it's not really JSON, is it? It's an object literal, that's all.
J-P
It is a bit of both - JSON is described in object literal notation.
Andrew Hare
It *is* JSON; that's what JSON is. object notation.
hasen j
Yes, okay, but it's not just that. I think there are a few technical differences. For example {a:1} is an object literal but it's NOT valid JSON. The JSON would have to be {"a":1} ...
J-P
I won't debate whether it is or isn't a JSON object, but I think it is a bit confusing (to someone new to JS) to mix the language concept of an anonymous object literal (`{ a: 1 }`) and what you can *compose* with that language concept (combine those anonymous object literals to create JSON).
Grant Wagner
+6  A: 

That is object literal notation. It is creating an object with a lhs property, set to val.

It is another way to do the following

var obj = new Object();
obj.lhs = val;
$.ajax(obj);

In jQuery, many functions take an options object, which is just a plain object with various properties set to determine how the function acts.

bdukes
I wouldn't call it a "shorthand". x = 5 is not a short hand for x = Integer(5);
hasen j
Well taken. Does that look more accurate?
bdukes
+4  A: 

It's a literal for an object.

var anObject = { member1: "Apple",
                 member2: function() { alert("Hello"); } };

alert(anObject.member1);      // Apple
anObject.member2();           // Hello
Georg
A: 

It's an anonymous object literal. In a basic sense, think of it as an associative array that uses "words" instead of number indexes.

In your case, you're submitting that object as the first (and only) parameter of the ajax method.

Brandon Belvin