[英]How do I specify generic type parameters for a function invocation in Closure?
[英]How can I specify the type of the “this” value for a closure in JetBrains IDEs?
JetBrains有一篇关于JSDoc注释的老博文,解释了如何通知IDE变量类型http://blog.jetbrains.com/webide/2012/10/validating-javascript-code-with-jsdoc-types-annotations/ 。
但是,我仍然无法找到告诉IDE许多jQuery回调中的“this”值是HTMLElements的方法。 例如:
/**
* Enable input
* @returns {SomeConstructor}
*/
SomeConstructor.prototype.enableInput = function(){
this.$markup.find('input').each(function(){
this.disabled = false;
});
return this;
};
以上示例仍将在IDE中生成警告 - “可能无效使用此”。
编辑:
在查看JSDoc文档后,我找到了@this注释http://usejsdoc.org/tags-this.html 。 @this将允许您为整个函数指定“this”值,但在发布的示例中,IDE将认为它返回的是HTMLElement而不是SomeConstructor。
@ de1mar在评论中钉了它。 诀窍是在关闭之前放置/ ** @ this {HTMLElement}。 所以,例如:
/**
* Enable input
* @returns {SomeConstructor}
*/
SomeConstructor.prototype.enableInput = function(){
this.$markup.find('input').each(/**@this {HTMLElement}*/function(){
this.disabled = false;
});
return this;
};
要么,
SomeConstructor.prototype.listenForCheck = function(){
this.$markup.find('input[type=checkbox]').on('click', /**@this {HTMLInputElement}*/ function(){
//Do something
});
};
对于那些在jetbrains IDE中编写jQuery的人来说应该是有用的。 谢谢@ del1mar!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.