我有一个可以接受可变数量参数的函数。
根据Google Closure Compiler wiki ,这是使用 @param
注解的方法。
/**
* Takes 2 or more strings and do something cool with them.
* @param {...string} var_args
* @return {string} the processed result
*/
function doSomethingCool() {
var len = arguments.length;
if (len < 2) {
throw Error('Need at least 2 arguments');
}
...
}
问题
当我尝试编译它时,我看到此警告:JSC_INEXISTENT_PARAM:参数 var_args 未出现在 doSomethingCool 的参数列表中第 6 行字符 1
所以我尝试了@param {string}参数
,但同样的错误。
我还尝试了没有变量名的@param {string}
。我得到:JSC_TYPE_PARSE_ERROR:错误的类型注释。需要 @param 标记中的变量名称。
问题
我做错了什么以及如何为闭包编译器注释可变参数?
最佳答案
您的 var_args
需要实际出现在参数列表中,这就是错误告诉您的内容。
/**
* Takes 2 or more strings and do something cool with them.
* @param {...string} var_args
* @return {string} the processed result
*/
function doSomethingCool(var_args) {}
闭包编译器将识别出它从未被引用,并在编译期间将其删除。
如果列出它让您感到困扰,您可以使用 @type
注释来代替:
/**
* Takes 2 or more strings and do something cool with them.
* @type {function(...string):string}
*/
function doSomethingCool() {}
如果您确实想要正确的类型检查,请注释该函数,以便它需要 2 个或更多字符串:
/**
* Takes 2 or more strings and do something cool with them.
* @type {function(string, string, ...string):string}
*/
function doSomethingCool() {}
关于javascript - Google Closure Compiler 用于变量参数的 @param 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45474397/