views:

2328

answers:

4

What is the correct syntax to create objects in javascript that will work across the majority of web browsers (by that I mean : IE 6+, Firefox 2+, Opera 9+ )

Is this valid


var a={ "class": "Person", 
        "name": "William Shakespeare",
        "birthday": -12802392000000, 
        "nickname": "Bill" 
       } ;

Or is this:


var a={ class: "Person", 
        name: "William Shakespeare",
        birthday: -12802392000000, 
        nickname: "Bill" 
       } ;

And what is the difference between the two?

+4  A: 

The spec say to use "".

Firefox accept without, but IE doesn't.

Pair is defined as

string : value

Value can be a string string is defined as

" chars "
AndreasN
IE does as well. Even IE6 accepts it.
Tomalak
yeah, i haven't noticed any problems with the unquoted method
nickf
+2  A: 

You have to use "quotes" around properties that contain anything other than word characters

{foo-bar: 3} // invalid
{"foo-bar": 3} // valid

Other than that there is no difference between the two and I'd imagine both ways work across all browsers

Gareth
+7  A: 

If IE fails with your second example it's because 'Class' is a reserved word (only in IE). Generally speaking it's always best to enclose your property names with quotes - doing this means it will ALWAYS work, whatever the circumstance.

J-P
class is actually a reserved word in all browsers. It's just that IE decides to fail when it's used.
Triptych
+7  A: 

@AndreasN is correct: the JSON specification dictates the use of quotes in order for it to actually be JSON. If you don't use quotes, it may be a valid object literal in Javascript, but it is not JSON. Other services besides browser-side Javascript use JSON (e.g. webservices using php, Java, etc.) and if you construct a string that lacks the quotes, there is no guarantee that it will be parsed correctly -- although I suspect most implementations would be robust enough to do so.

FYI it is dangerous in Javascript to directly use eval() on JSON strings from sources which you cannot prevent malicious attacks. Again, see the JSON site which gives more of an explanation as well as a very short javascript file which safely parses JSON strings into Javascript objects.

edit: I guess technically your original question is not about JSON but rather the Javascript's syntax for object literals. The difference is that objects constructable from a JSON string will exclude many other possible object literals, e.g.:

var a = {cat: "meow", dog: "woof"};
var aname = {cat: "Garfield", dog: "Odie"};
var b = {
  counter: 0,
  pow: function(x) { return x+1; },
  zap: function(y) { return (counter += y); }
};
var c = {
  all: [a,aname],
  animals: a,
  names: aname,
};

Object literals "a" and "aname" can be expressed in JSON (by adding quotes to the property names). But object literals "b" and "c" cannot. Object literal "b" contains functions (not allowed in JSON). Object literal "c" above contains references to other variables in a way that is not representable in JSON because some of the references are shared. If you make a change to c.names it will also change c.all[1] since they share a reference to the same variable. JSON can only express objects that have a tree structure (e.g. each sub-element of the overall object is independent).

Jason S