Return to Snippet

Revision: 58725
at July 30, 2012 22:07 by timsommer

Initial Code
var myModule = {
  myProperty: 'someValue',
  // object literals can contain properties and methods.
  // here, another object is defined for configuration
  // purposes:
  myConfig: {
    useCaching: true,
    language: 'en'
  // a very basic method
  myMethod: function () {
    console.log('I can haz functionality?');
  // output a value based on current configuration
  myMethod2: function () {
    console.log('Caching is:' + (this.myConfig.useCaching) ? 'enabled' : 'disabled');
  // override the current configuration
  myMethod3: function (newConfig) {
    if (typeof newConfig == 'object') {
      this.myConfig = newConfig;
myModule.myMethod(); // I can haz functionality
myModule.myMethod2(); // outputs enabled
  language: 'fr',
  useCaching: false
}); // fr

Initial URL

Initial Description
In object literal notation, an object is described as a set of comma-separated name/
value pairs enclosed in curly braces ({}). Names inside the object may be either strings or identifiers that are followed by a colon. There should be no comma used after the final name/value pair in the object as this may result in errors.

Initial Title
Javascript Object Literal

Initial Tags

Initial Language