JavaScript Tips

XUL Tips

  • Kiedy wpisywany jest kod w formacie XUL, opatrz funkcje i zmienne w srodku obiektu unikalna nazwa, aby uniknac konfliktu z istniejaca nazwa funcji lub zmiennej
var UniqueName = {
  _privateMember: 3,
  publicMember: "A string",

  init: function() {
    this.doSomething(this.anotherMember);
  },

  doSomething: function(aParam) {
    alert(aParam);
  }
};

XPConnect

  • Nie uzywaj metod i wlasciwosci wiecej niz tego potrzbujesz. Bardzo czesto szybciej jest przechowywac wynik w tymczasowej zmiennej.
  • Nie nazywaj metod ktorych nie musisz nazywac. Na przyklad, windowManager.getEnumerator(aType).hasMoreElements() moze byc zastapiony windowManager.getMostRecentWindow(aType) != null for a single window.
  • Nie wchodz w interfejsy chyba ze potrzebujesz dostepu do metod i wlasciwosci tego interfejsu. Nie musisz wchodzic w interfejsy, aby porownywac objekty, ani podawac objektow jako parametry (Dziala to inaczej niz w C++, w ktorym interfejsy o ktorych mowa sa niezbedne.
  • Nie nazywaj QueryInterface chyba ze wiesz co robisz. Zamiast tego uzywaj instanceof, przyklad:
if (target instanceof Components.interfaces.nsIRDFResource)
  return target.Value;
if (target instanceof Components.interfaces.nsIRDFLiteral)
  return target.Value;
return null;
  • Nie testuj wartosci zwrotnej QueryInterface, zwraca zawsze orginalna zmienna jesli sie powiedzie.
  • When passing an object to an XPCOM method it is helpful if the object you pass is an XPCOM object, so that the C++ method access a C++ object. However, this is not always necessary or desirable. For instance the offline observer declared above is a JavaScript object that is registered with an XPCOM object, so that the call back from XPCOM executes the JavaScript method. Some XPCOM methods expect an object that implements several interfaces thus requiring you to write a QueryInterface method. However, in JavaScript this is quite simple even in the case of a weak reference which in C++ requires a helper class:
var weakObserver = {
  QueryInterface: function QueryInterface(aIID) {
    if (aIID.equals(Components.interfaces.nsIObserver) ||
        aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
        aIID.equals(Components.interfaces.nsISupports))
       return this;
    throw Components.results.NS_NOINTERFACE;
  },
  observe: function observe(aSubject, aTopic, aState) {
  }
}
  • When declaring XPCOM methods, try to use the same names for method parameters as are used in the interface definition.

DOM elements

  • DOM elements are just XPCOM objects with some of the interfaces precached.
  • Don't call getAttribute to see if an attribute exists, call hasAttribute instead.
  • Prefer to loop through childNodes rather than using first/lastChild with next/previousSibling. But prefer hasChildNodes() to childNodes.length > 0. Similarly prefer document.getElementsByTagName(aTag).item(0) != null to document.getElementsByTagName(aTag).length > 0.
  • Prefer to use localName rather than tagName.
  • XUL elements have many of the attributes mapped to properties. This was done for a reason, so use them! The properties are:
    • align
    • allowEvents
    • contextMenu
    • datasources
    • dir
    • flex
    • height
    • id  
    • left
    • maxHeight
    • maxWidth
    • minHeight
    • minWidth
    • observes
    • orient
    • pack
    • persist
    • ref
    • statusText
    • top
    • tooltip
    • tooltipText
    • width
  • XUL also maps the ordinal attribute but this defaults to "1" if it is not present.
  • XUL also maps the class attribute, but unfortunately class is a reserved identifier, so the property is named className. (The property could have been implemented as ["class"'] but that just looks silly.)
  • XUL also maps the hidden and collapsed attributes to properties, but note that these are boolean properties whereas the above list are all string properties.
  • XUL also maps other useful properties and methods using XBL bindings; these vary from element to element.
  • For best performance give ids to all important elements. However in addition to locating elements by tag name XUL also allows you to locate an element by attribute, starting at any element in the document.

References