为了避免使用new
在我的 JavaScript 代码中,我编写工厂来创建对象。
我尝试了很多种组合,最令我满意的结果如下:
/**
* Document module
* @module app/document
*/
(function () {
'use strict';
/**
* Factory that creates a document object.
* @alias module:app/document.factory
* @return {document}
*/
function document() {
/**
* Get document id
* @method id
* @return {String}
*/
var id = function id() {...},
api = {
id: id
};
return api;
}
/**
* This module exports the {@link module:app/document.factory|factory} function.
*/
module.exports = document;
}());
这些评论的问题是没有document
对象定义。因此,我无法在另一个对象中引用该对象,并且在扩展该对象时无法继承其文档。
记录此类对象的适当方法是什么?
如果我使用@typedef
标签,我得到静态factory
方法
和document
正确记录的对象
但不是id
方法文档由 JSDoc 生成:
/**
* Document module.
* @module app/document
*/
(function () {
'use strict';
/**
* Factory that creates a document object.
* @function module:app/document.factory
* @return {document}
*/
function factory(agent) {
/**
* @callback document~id
* @returns {String}
*/
var id = function id() {...},
/**
* @typedef document
* @property {document~id} id
*/
document = {
id: id
};
return document;
}
module.exports = factory;
}());
我对您的建议是使用 @typedef 定义类型,然后使用 @type {FactoryDefinition} 注释 module.exports = factory 来定义模块上的导出
/** @typedef {{ id: !string }} */
var DocumentDefinition;
/** @typedef {!function(!object):!DocumentDefinition} */
var FactoryDefinition;
/** @type {FactoryDefinition} */
module.exports = factory
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)