Songbird Wiki > Developer Center > Articles > Style Manual > Javascript Guidelines

Javascript Guidelines

From $1

The Joy of Javascript.

JavaScript Coding Standards

  • try { /* ... */ } catch(e) {} IS BAD. Please don't ever do this.

Generic JavaScript Module Boilerplate

This is wrong and out of date! Use XPCOMUtils.jsm! 

Foo.js:

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;

const CLASSID    = Components.ID("{7ad52bf0-941f-4945-8d86-b9d3fadfe955}");
const CLASSNAME  = "Foo Class";
const CONTRACTID = "@songbirdnest.com/test/foo;1";
const INTERFACES = [Ci.nsISupports, Ci.nsIClassInfo, Ci.sbIFoo];
const CI_FLAGS = 0;

function Foo() {
}

const CONSTRUCTOR = Foo;

Foo.prototype = {
  _title: null,
  get title()         { return this._title; },
  set title(newValue) { this._title = newValue; }

#include ../../GenericClassInfo.js
#include ../../GenericQueryInterface.js
};

#include ../../GenericModule.js
#include ../../GenericFactory.js

GenericClassInfo.js:

  ,
  getInterfaces: function CI_getInterfaces(countRef) {
    countRef.value = INTERFACES.length;
    return INTERFACES;
  },
  getHelperForLanguage: function CI_getHelperForLanguage(language) {
    return null;
  },
  contractID: CONTRACTID,
  classDescription: CLASSNAME,
  classID: CLASSID,
  implementationLanguage: Ci.nsIProgrammingLanguage.JAVASCRIPT,
  flags: CI_FLAGS,

  QueryInterface: function CI_QueryInterface(iid) {
    if(INTERFACES.some(function(element) { return iid.equals(element); })) {
      return this;
    }
    throw Cr.NS_ERROR_NO_INTERFACE;
  }

GenericQueryInterface.js:

  ,
  QueryInterface: function QI_QueryInterface(iid) {
    if(INTERFACES.some(function(element) { return iid.equals(element); })) {
      return this;
    }
    throw Cr.NS_ERROR_NO_INTERFACE;
  }

GenericModule.js:

var Module = {
  QueryInterface: function M_QueryInterface(iid) {
    if(INTERFACES.some(function(element) { return iid.equals(element); })) {
      return this;
    }
    throw Cr.NS_ERROR_NO_INTERFACE;
  },

  getClassObject: function M_getClassObject(cm, cid, iid) {
    if(!iid.equals(Ci.nsIFactory)) {
      throw Cr.NS_ERROR_NOT_IMPLEMENTED;
    }
    if(cid.equals(CLASSID)) {
      return new GenericComponentFactory(CONSTRUCTOR);
    }
    throw Cr.NS_ERROR_NO_INTERFACE;
  },

  registerSelf: function M_registerSelf(cm, file, location, type) {
    var cr = cm.QueryInterface(Ci.nsIComponentRegistrar);
    cr.registerFactoryLocation(CLASSID, CLASSNAME, CONTRACTID,
                               file, location, type);
  },

  unregisterSelf: function M_unregisterSelf(cm, location, type) {
    var cr = cm.QueryInterface(Ci.nsIComponentRegistrar);
    cr.unregisterFactoryLocation(CLASSID, location);
  },

  canUnload: function M_canUnload(cm) {
    return true;
  }
};

function NSGetModule(cm, file) {
  return Module;
}

GenericFactory.js:

function GenericComponentFactory(ctor, params) {
  this._ctor = ctor;
  this._params = params;
}
GenericComponentFactory.prototype = {
  _ctor: null,
  _params: null,

  createInstance: function GCF_createInstance(outer, iid) {
    if (outer != null)
      throw Cr.NS_ERROR_NO_AGGREGATION;
    return (new this._ctor(this._params)).QueryInterface(iid);
  },

  QueryInterface: function GCF_QueryInterface(iid) {
    if (iid.equals(Ci.nsIFactory) ||
        iid.equals(Ci.nsISupports)) 
      return this;
    throw Cr.NS_ERROR_NO_INTERFACE;
  }
};

 

Tags:
 
Images (0)
 
Comments (0)
You must login to post a comment.