如何使用Closure Compiler在函数中定义“this”的类型

问题描述 投票:0回答:1

我的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的答案,但这似乎没有做我认为它做的事情。

如何为Closure Compiler指定this的类型?

javascript this google-closure-compiler
1个回答
1
投票

感谢@RandyCasburn的评论

/** function(this:Element) */应更改为** @this {Element} */,以基本上将函数的上下文类型转换为Element

我认为应该使用/** function(this:Element) */来定义具有元素上下文的函数的参数类型

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.