2014-04-08 36 views
2

假設我們正在構建一個嵌套命名空間,我們將根據需要對多個對象進行原型設計。默認情況下,對象window.blah.foo是空的。然後,我們將所有子對象和它們的功能原型化爲一個原型。當我們在下面進行記錄時,我們將所有內部函數顯示爲全局變量。現在想象你有幾十個以相同方式構建的文件。我們如何記錄這一點,以便.bar1(),bar2()和bar3()函數是window.blah.foo對象的方法,特別是當它的實例化發生在該文件之外時,可能多次。記錄原型繼承的JSDoc

/** 
* Blah - Our very own global Namespace 
* @namespace 
* @type {object} 
* @global 
* @public 
*/ 
window.blah = window.blah || {}; 

/** 
* Immediately-Invoked Function Expression (IIFE). 
* @function 
* @param {object} w - Global window object. 
* @returns {Object} window.blah.foo 
*/ 
(function (w) { 

    // strict JS 
    'use strict'; 

    /** 
    * Create instance of w.blah.foo constructor 
    * @constructor 
    */ 
    w.blah.foo = function() { 

    }; 

    /** 
    * Append properties and methods to an instance of window.blah.foo 
    * @constructor 
    */ 
    w.blah.foo.prototype = { 

     /** 
     * Dummy function to return the number 1. 
     * @method bar1 
     */ 
     bar1: function() { 
      console.log(1); 
     }, 

     /** 
     * Dummy function to return the number 2. 
     * @method bar2 
     */ 
     bar2: function() { 
      console.log(2); 
     }, 

     /** 
     * Dummy function to return the number 3. 
     * @method bar3 
     */ 
     bar3: function() { 
      console.log(3); 
     } 

    }; 

}(window)); 

回答

3

以下將移動它們所屬的barX方法。請注意,jsdoc無法處理記錄IIFE的doclet。獲得正確方法的關鍵是/** @lends blah.foo# */。請參閱@lends的文檔。 #告訴jsdoc借給blah.foo的項目屬於該類的一個實例。有關更多詳細信息,請參閱namepaths上的文檔。

/** 
* Blah - Our very own global Namespace 
* @namespace 
* @global 
*/ 
window.blah = window.blah || {}; 

/** 
* Immediately-Invoked Function Expression (IIFE). 
* @function 
* @param {object} w - Global window object. 
* @returns {Object} window.blah.foo 
*/ 
(function (w) { 
    // strict JS 
    'use strict'; 

    /** 
    * Create instance of w.blah.foo constructor 
    * @constructor 
    * @name blah.foo 
    */ 
    w.blah.foo = function() { 

    }; 

    /** @lends blah.foo# */ 
    w.blah.foo.prototype = { 

     /** 
     * Dummy function to return the number 1. 
     */ 
     bar1: function() { 
      console.log(1); 
     }, 

     /** 
     * Dummy function to return the number 2. 
     */ 
     bar2: function() { 
      console.log(2); 
     }, 

     /** 
     * Dummy function to return the number 3. 
     */ 
     bar3: function() { 
      console.log(3); 
     } 

    }; 

}(window)); 
+0

謝謝!我知道某處已經錯過了一個標籤。 –