我的项目的闭包编译器的目标是实现 100% 的类型化,但我正在努力解决一个简单的 jQuery 模式,如下所示:
$Controls.on('change input', /** function(this:Element) */ function () {
$(this).removeClass('is-invalid').next('.invalid-feedback').remove();
});
无论有没有注释 /** function(this:Element) */
,我都会收到以下错误:
js/quote.js:61: WARNING - could not determine the type of this expression
$(this).removeClass('is-invalid').next('.invalid-feedback').remove();
^^^^
我想我从 Github Wiki titled 'Types in the Closure Type System' 找到了答案在“函数 this
类型”部分中,但这似乎并没有达到我的预期。
如何为闭包编译器指定 this
的类型?
最佳答案
感谢@RandyCasburn 的评论
/** function(this:Element) */
应更改为 ** @this {Element} */
以本质上强制转换上下文的类型Element
我认为应该使用 /** function(this:Element) */
来定义具有元素上下文的函数的参数类型
关于javascript - 如何使用闭包编译器在函数中定义 "this"的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50282347/