JSDoc CommonJS将导出对象传递给IIFE

时间:2015-07-27 20:58:47

标签: javascript jsdoc jsdoc3

更新:@spenibus帮助我得出结论,这可能是JSDoc本身的一个问题。我在GitHub上将我的发现添加到了this open issue。 @spenibus找到了一个解决方案,但它需要略微改变的IIFE版本

我在CommonJS模块中使用IIFE,以便能够使用CommonJS,并且如果module.exports不存在则回退以将接口分配给窗口对象。如何正确记录这一点,以便传入的exports对象被视为module.exports?

=OR(EXACT(F2, $B$2:$B$595))

1 个答案:

答案 0 :(得分:3)

虽然JSDoc issue 456似乎与我们在任何地方都没有相关。

我看了Use JSDoc: @alias,虽然很有希望,却没有提供相同的JSDoc输出。

然后我尝试了一些简单的东西,让我在脑海里玩FF7胜利主题,AKA它有效:

/**
 * This is a description
 * @module someModule
 */

(function() {

    // export to window when not used as a module
    if(typeof exports === 'undefined') {
        var exports = window;
    }

    /**
     * Returns true if something.
     * @param {String} type
     * @returns {boolean}
     * @static
     */
    exports.isSomething = function(type){
        return true;
    };
})();

在项目目录上使用jsdoc ./会产生相同的输出,就好像我没有使用过IIFE一样。基本思想是始终拥有一个名为exports的对象,并简单地修改它引用的内容。

Nodejs test

var mm = require('./module.js');

console.log('--Testing nodejs--');
console.log(mm);

输出:

--Testing nodejs--
{ isSomething: [Function] }

Html脚本测试

<script src="module.js"></script>
<script>
    console.log('--html script test--');
    console.log(isSomething.toString());
</script>

输出:

"--html script test--"
"function (type){
    return true;
}"
  

更新2015-08-13 05:10 +0000
  在IIFE中移动了窗口导出,以避免在html脚本中出现额外的exports var。