EBay的VJET和Google的Closure Compiler都在Javascript评论中使用了类型注释。
为什么他们选择了不兼容的语法?
VJET
function add(a, b) { //< Number add(Number, Number)
return a + b ;
}
Google Closure
/**
* Queries a Baz for items.
* @param {number} groupNum Subgroup id to query.
* @param {string|number|null} term An itemName,
* or itemId, or null to search everything.
*/
goog.Baz.prototype.query = function(groupNum, term) {
...
};
有没有办法自动将VJET类型的注释转换为Google Closure类型注释?
答案 0 :(得分:1)
没有自动方式将vjetdocs转换为jsdocs,但这里是查询api的手动翻译。首先,我将从闭包的jsdoc转换为vjetdoc,然后再回来向您展示差异。
/**
* Queries a Baz for items.
* @param {number} groupNum Subgroup id to query.
* @param {string|number|null} term An itemName,
* or itemId, or null to search everything.
*/
VJET doc中的groupNum是第一个位置,类型是Number(不是数字)vjet使用区分大小写的EcmaScript标准类型名称。
itemName是VJET doc是混合类型,可以是Number或String。目前,VJETDoc没有名为Null的默认类型。如果存在Null数据类型,则它将是大写的。我相信这应该添加到VJET,但是可以传入null,并且VJET不会抱怨下面的声明。
您可以使用或不使用函数和参数名称在vjetdoc中编写它(如果您不想重复自己......位置接管。
// shortest form
function query(groupNum, itemName) { //< void (Number, {String|Number}?)
}
//
// longer form - on same line as declaration
function query(groupNum, itemName) { //< void query(Number groupNum, {String|Number}? itemName)
}
// longer form mixed with jsdocs
/**> void query(Number groupNum, {String|Number}? itemName);
*
* Queries a Baz for items.
* @param {number} groupNum Subgroup id to query.
* @param {string|number|null} term An itemName,
* or itemId, or null to search everything.
*/
function query(groupNum, itemName) {
}
query(10,"test");
query(30,20);
query(20,null);