2016-02-05 102 views
1

我有返回對象的函數。我希望把他們的jsdoc定義在webstorm intellisense上看到他們的屬性和方法。JSDOC和揭示模塊模式

我該如何編寫jsdoc的以下函數?

function MyOtherFunc() { 
    return { a:'for eg string', b:12 } 
} 

function MyFunc() { 
    var prop = MyOtherFunc(); 

    function myMethod() { 
    alert('my method'); 
    } 

    function myOtherMethod() { 
    alert('my other method'); 
    } 

    // explicitly return public methods when this object is instantiated 
    return { 
    someMethod : myMethod, 
    someOtherMethod : myOtherMethod 
    };  
} 

回答

0

這個確切的情況下在WebStorm妥善處理,而不JSDoc太多,但你可以使用closure compiler type syntax這個:

/** 
* @return {{a: string, b: number}} 
*/ 
function MyOtherFunc() { 
    return { a:'for eg string', b:12 } 
} 
0

你應該能夠完成你想要使用@namespace@memberof@instance什麼。

/** 
* @namespace MyFunc 
*/ 
function MyFunc() { 
    var prop = MyOtherFunc(); 

    /** 
    * Does myMethody things 
    * 
    * @memberof MyFunc 
    * @instance 
    */ 
    function myMethod() { 
    alert('my method'); 
    } 

    /** 
    * Does myOtherMethody things 
    * 
    * @memberof MyFunc 
    * @instance 
    */ 
    function myOtherMethod() { 
    alert('my other method'); 
    } 

    // explicitly return public methods when this object is instantiated 
    return { 
    someMethod : myMethod, 
    someOtherMethod : myOtherMethod 
    };  
} 

如果MyFunc有任何靜態方法,你只離開過@instance或明確地@static對它進行標記。

/** 
* Say hello 
* 
* @param {string} [name] - Who to say hello to 
* @memberof MyFunc 
* @static 
*/ 
MyFunc.helloWorld = function (name) { 
    console.log('Hello ' + (name || 'world')); 
} 

在這種情況下,使用@memberof@static應該是可選的。解析器應該能夠自己解決這個問題;但是,無論如何,您都可以明確地使用它們,以便讓讀者明白任何代碼。

+0

我已經完成了這個過去,但最近很難記住我之前做過的。試圖弄明白的時候遇到了你的問題。 :-) –