views:

204

answers:

2

This is something of an embarrassing question to ask, seeing as it is mainly about my lack of Javascript knowledge. Seems like it might be "so obvious" that nobody specifies it anymore. Gotta start somewhere! I'm a proficient coder in other languages, I'm just unfamiliar with JS.

On the ReCaptcha Client API docs page, there's an example of the proper format for the customization options dictionary.

I'm writing a 3rd party app which generates that dictionary based on user prefs. I don't know of / couldn't find a dictionary type in Javascript. My best guess is that the dictionary is JSON. I want to make sure that my output is valid and conformant to what they expect, particularly with regards to quotes (at all? single? double? etc.)

Is it JSON? Is it another type of dictionary? Can anybody point me at some kind of specs for how a valid instance of a JS dictionary looks? Googling for "Javascript Dictionary" has proved ineffective owing to the generic quality of the search terms.

Thanks!

+1  A: 

It's a javascript object literal, which is almost the same as JSON (it's a superset). Any valid JSON will work here, but as you can see from their example, Javascript has a few other options:

<script>
var RecaptchaOptions = {
   theme : 'white',
   tabindex : 2
};
</script>

The part between the braces is valid Javascript but not valid JSON because theme, tabindex and white should all be double-quoted. Below is valid JSON embedded in Javascript, which works perfectly:

<script>
var RecaptchaOptions = {
   "theme" : "white",
   "tabindex" : 2
};
</script>
Greg
A: 

No, it’s not JSON, it’s plain JavaScript. More exact: It is a simple object declared using the object initialiser syntax { … }.

That syntax is similar to JSON as JSON is a subset of JavaScript. But JSON requires the property names to be a quoted string, JavaScript not.

Gumbo