ejf*_*cis 5 javascript jsdoc jsdoc3
更新:@spenibus帮助我得出结论,这可能是JSDoc本身的一个问题.我在他们的GitHub上将我的发现添加到了这个公开的问题中.@spenibus找到了一个解决方案,但它需要略微改变版本的IIFE
我在CommonJS模块中使用IIFE,以便能够使用CommonJS和后退,如果module.exports不存在,则将接口分配给窗口对象.如何正确记录这一点,以便传入的exports对象被视为module.exports?
/**
* 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
);
Run Code Online (Sandbox Code Playgroud)
虽然JSDoc 问题 456似乎很相关,但我们还没有取得任何进展。
我查看了Use JSDoc: @alias,虽然很有希望,但没有提供相同的 JSDoc 输出。
然后我尝试了一些简单的方法,让我在脑海中播放 FF7 的胜利主题曲,又名它起作用了:
/**
* 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;
};
})();
Run Code Online (Sandbox Code Playgroud)
在项目目录上使用jsdoc ./会产生与我没有使用 IIFE 相同的输出。基本思想是始终命名一个对象exports并简单地修改它引用的内容。
var mm = require('./module.js');
console.log('--Testing nodejs--');
console.log(mm);
Run Code Online (Sandbox Code Playgroud)
输出:
--Testing nodejs--
{ isSomething: [Function] }
Run Code Online (Sandbox Code Playgroud)
<script src="module.js"></script>
<script>
console.log('--html script test--');
console.log(isSomething.toString());
</script>
Run Code Online (Sandbox Code Playgroud)
输出:
"--html script test--"
"function (type){
return true;
}"
Run Code Online (Sandbox Code Playgroud)
更新 2015-08-13 05:10 +0000
将窗口导出移至 IIFE 内,以避免exports在 html 脚本中放置额外的 var。