JavaScript 编程技巧

翻译不完整。 请帮助我们翻译这篇文章!

XUL 技巧

  • 在使用 XUL 覆盖层来插入代码时, 使用一个唯一的名称将函数和变量封装在对象内部,以避免与当前或未来的函数,变量名称发生冲突。
var UniqueName = {
  _privateMember: 3,
  publicMember: "A string",

  init: function UN_init() {

  doSomething: function UN_doSomething(aParam) {


  • Don't use object methods and properties more than you have to. It is often faster to store the result in a temporary variable.
  • Don't call methods that you don't have to. For instance, windowManager.getEnumerator(aType).hasMoreElements() may be replaced with windowManager.getMostRecentWindow(aType) != null for a single window.
  • Don't query interfaces unless you need to access methods and properties of that interface. You do not have to query interfaces to compare objects, nor to pass objects as parameters (This is different from C++, where you do have to query interfaces in both cases).
  • Don't call QueryInterface unless you expect to succeed. Instead, use instanceof, e,g,:
if (target instanceof Components.interfaces.nsIRDFResource)
  return target.Value;
if (target instanceof Components.interfaces.nsIRDFLiteral)
  return target.Value;
return null;
  • Don't test the return value of QueryInterface, it always returns the original variable if it succeeds. XPConnect knows all about tearoffs and modifies the object that you QueryInterface or instanceof to cache all its known interfaces.
  • 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) ||
       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 元素

  • DOM 元素只是带有一些预缓存的接口的 XPCOM 对象。
  • 不要调用 getAttribute 方法来检查属性是否存在,使用 hasAttribute 代替。
  • 倾向于通过childNode 循环的方式去遍历, 而不是对 first/lastChild 使用 next/previousSibling 。 使用 hasChildNodes() 而不是 childNodes.length > 0. 类似的,使用 document.getElementsByTagName(aTag).item(0) != null 而不是 document.getElementsByTagName(aTag).length > 0.
  • 使用 localName 而不是 tagName.
  • XUL 元素有许多特性与属性相映射。这样做是有原因的,所以要使用它们!属性有:
    • id
    • align
    • dir
    • flex
    • orient
    • pack
    • observes
    • contextMenu
    • tooltip
    • width
    • height
    • minWidth
    • minHeight
    • maxWidth
    • maxHeight
    • persist
    • left
    • top
    • datasources
    • ref
    • tooltipText
    • statusText
    • allowEvents
  • 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.