JavaScript Best Practices: Objects

  • Use the literal syntax for object creation.
    // bad
    var item = new Object();
    
    // good
    var item = {};
    
  • Don’t use reserved words as keys.
    // bad
    var superman = {
      class: 'superhero',
      default: { clark: 'kent' },
      private: true
    };
    
    // good
    var superman = {
      cssClass: 'superhero',
      defaults: { clark: 'kent' },
      hidden: true
    };
    
  • Use readable synonyms in place of reserved words.
    // bad
    var superman = {
      class: 'alien'
    };
    
    // bad
    var superman = {
      klass: 'alien'
    };
    
    // good
    var superman = {
      type: 'alien'
    };
    
 

austinvernsonger