JSDoc CommonJS with exports object passed into IIFE
更新:@spenibus帮助我得出结论,这可能是JSDoc本身的问题。我在他们的GitHub上将此发现添加到了此公开问题中。 @spenibus找到了一个解决方案,但它需要IIFE
的稍微改动的版本
我在CommonJS模块中使用IIFE,以便能够与CommonJS一起使用,如果没有module.exports,则回退到将接口分配给window对象。如何正确记录此文件,以便将传入的导出对象视为module.exports?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /** * This is a description * @module someModule */ (function (exports) { /** * Returns true if something. * @param {String} type * @returns {boolean} * @static */ var isSomething = function isSomething(type){ return true; }; exports.isSomething = isSomething; })( //if exports exists, this is a node.js environment so attach public interface to the `exports` object //otherwise, fallback to attaching public interface to the `window` object (typeof exports === 'undefined') ? window : exports ); |
尽管JSDoc第456期似乎很相关,但仍未使我们走上任何一条路。
我看过Use JSDoc:@alias,尽管很有希望,但它没有提供相同的JSDoc输出。
然后我尝试了一些简单的操作,使我脑海中发挥了FF7胜利主题,又奏效了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * 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; }; })(); |
在项目目录上使用
Node.js测试
1 2 3 4 | var mm = require('./module.js'); console.log('--Testing nodejs--'); console.log(mm); |
输出:
1 2 | --Testing nodejs-- { isSomething: [Function] } |
HTML脚本测试
1 2 3 4 | <script src="module.js"> console.log('--html script test--'); console.log(isSomething.toString()); |
输出:
1 2 3 4 | "--html script test--" "function (type){ return true; }" |
Update 2015-08-13 05:10 +0000
Moved window exportation inside the IIFE to avoid extraexports var laying around in html script.