Mr.*_*bin 10 javascript javadoc google-closure google-closure-compiler
我在使用jQuery-1.4.3 externs文件时特别看到了这一点.这个javadocs读取
/**
* @param {(string|number|function(number,number))=} arg1
* @return {(number|jQueryObject)}
* @nosideeffects
*/
jQueryObject.prototype.width = function(arg1) {};
Run Code Online (Sandbox Code Playgroud)
我有一个看起来像这样的电话:
var w = $(window).width();
$('#whatever').width(w)
Run Code Online (Sandbox Code Playgroud)
Closure抱怨:警告 - jQueryObject.prototype.height的实际参数1与找到的形式参数不匹配:( jQueryObject | null | number)required :( function(number,number):?| number | string | undefined)$('# .Xobni的')的高度($(窗口).height());
从玩游戏(删除可能的返回类型),我可以看到问题是第一次调用width可能返回一个jQueryObject,因为那不是一个有效的输入,Closure给了我一个错误.我尝试添加这个:
/**
* @type {number}
*/
var w = $(window).width();
$('#Xobni').width(w);
Run Code Online (Sandbox Code Playgroud)
但是然后Closure抱怨:警告 - 初始化变量found:(jQueryObject | null | number)required:number var w = $(window).width();
问题是当width接受一个参数时,它返回一个jQueryObject.当它不参数时,它返回一个数字.所以我知道我的电话没问题,但javadocs并没有完全反映出来,所以Closure警告我.有没有办法适当地修复javadoc或告诉Closure我知道这个结果将是一个数字.我知道我可以抑制错误,但我想知道如何更好地注释这些事情.
谢谢您的帮助!
您可以按如下方式覆盖:
var w = /** @type {number} */ ($(window).width());
$('#whatever').width(w)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2792 次 |
| 最近记录: |