tags:

views:

89

answers:

2

I've tried looking to see if this is possible, but I can't find my answer.

I'm trying to get the following to work:

var defaults = {
 'background-color': '#000',
 color: '#fff',
 weekdays: {['sun','mon','tue','wed','thu','fri','sat']}
};

It just gives an error, and I've tried using ({...}) and [{...}] I'd like to be able to access the weekdays using somthing like:

defaults.weekdays[0];

is this possible?

+4  A: 
// define
var foo = {
  bar: ['foo', 'bar', 'baz']
};

// access
foo.bar[2]; // will give you 'baz'
Eimantas
+10  A: 

Kill the braces.

var defaults = {
 backgroundcolor: '#000',
 color: '#fff',
 weekdays: ['sun','mon','tue','wed','thu','fri','sat']
};
Stefan Kendall
Also make sure that you don't have a trailing comma after the end of the final ], or your code will break in IE7.
Stefan Kendall
...and only IE7 :(
Stefan Kendall
Ahhh ok thank you!
fudgey
@stefan IE6 too ;)
Justin Johnson
Really? I've only seen breakage in IE7, and I see it every time I refactor. Every single time. Perhaps IE6's behavior is nondeterministic. That's worse than failing every time, right?
Stefan Kendall
I get "missing : after property id"
Roatin Marth
This works for me, as long as you put quotes around "background-color", as Crescent Fresh mentioned above.
Nosredna
Don't use the dash, or use quotes as mentioned.
Stefan Kendall
If you have a comma after the last item in an Array or Object, it is invalid JavaScript syntax. JSLint will automate these checks for you: http://www.jslint.com/lint.html
Matt Ryall
This was actually fixed by integrating YUI Compressor into my build system. Trailing commas disappear.
Stefan Kendall