views:

115

answers:

4

I'm trying to learn about this feature of javascript I keep seeing in code, but I don't know the name of the construction to google for...

var Stats = {
  onLoad: function(e) {
    // content
    this.variable++;
  },

  variable: 1
};

Is this way of organising functions and variables based on JSON?

+8  A: 

It's an "Object Literal" - see the JavaScript Guide.

RichieHindle
+4  A: 

It's called the Object Literal syntax.
It's a superset of JSON.

Greg
+1  A: 

The sample code creates an object literal (i.e. a hash map), with two entries. The first entry maps "onLoad" to an anonymous function, and the second entry maps "variable" to 1.

Barry Kelly
+1  A: 

Thanks guys, I gave the answer to Richie for being first, but the link Greg gave has more examples, so I'll use both to learn of course (and others from google).

@Barry: thanks for explaining what it does, but that was not really what I was aiming at with the question :)

Tominator