javascript - 如何使用闭包编译器在函数中定义 "this"的类型

标签 javascript this google-closure-compiler

我的项目的闭包编译器的目标是实现 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/

相关文章:

javascript - 从另一个js文件获取数组条目

jquery - $(this) 不工作 x-可编辑

select - <select> 超链接列表 : onchange ="location=this.options[this.selectedIndex].value" or onchange ="location=this.value"

c++ - 是否可以默认传递 "this"?

javascript - 使用 Closure Compiler,Array<T> 注释与仅 Array 相比有什么好处?

javascript - 闭包编译器 : could not determine the type of this expression at callback

php - 网络应用架构设计

javascript - 使用 JQuery 检查表单

javascript - ExtJS ComboBox triggerAction : "all" really do? 是什么意思

javascript - 在 Google App Engine 中使用 Google Closure 的 "compiler.jar"