A few months ago I came across a post by Dustin Diaz titled JSON for the masses. As some comments in the post suggest, his examples are not actually JSON (commonly used as a data interchange format) because the member names are not quoted. But the main point of the article is actually on JavaScript’s “object literal notation”, a method of creating object literals.

It’s quite simple. Douglas Crockford writes:

In the object literal notation, an object description is a set of comma-separated name/value pairs inside curly braces. The names can be identifiers or strings followed by a colon.

Thus, you can create an object like this:

var myObject = {
    name: "Peter Foti",
    'course': 'JavaScript',
    grade: 'A',
    level: 3
};

At first, I found this style somewhat awkward. But the more I use it, the more natural it seems. It’s also quite useful for creating self-invoking objects or namespaces to keep your global objects to a minimum. For example:

var FOTI = {
    return {
        name : "Peter Foti",
        init : function() {
            alert('Initializing!');
        }
    };
}(); // Self invoking
window.onload = FOTI.init;