简体   繁体   English

强制NetBeans为假定特定类型的JavaScript变量提供代码完成

[英]Force NetBeans to provide code completion for a JavaScript variable assuming a specific type

I am using NetBeans, specifically the HTML5 & PHP bundle. 我正在使用NetBeans,特别是HTML5和PHP包。 Currently I'm working on a game which makes use of the HTML5 canvas element. 目前我正在开发一款利用HTML5 canvas元素的游戏。 I have a global variable called context : 我有一个名为context的全局变量:

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

Obviously, the IDE has no idea what type context is, since it cannot find out the type of canvas in the first place and it can't possibly know what getContext() is going to return when called on that canvas . 显然,IDE不知道什么类型的context ,因为它首先找不到canvas的类型,并且在该canvas上调用时它不可能知道getContext()将返回什么。 I would like NetBeans to provide code completion feature for context assuming it represents a specific type (namely, CanvasRenderingContext2D ). 我希望NetBeans为context提供代码完成功能,假设它代表一种特定的类型(即CanvasRenderingContext2D )。 After doing some research I found this: https://blogs.oracle.com/netbeansphp/entry/defining_variable_type_in_a . 做了一些研究后我发现了这个: https//blogs.oracle.com/netbeansphp/entry/defining_variable_type_in_a The site states that such feature can be used in PHP by providing a PHPDoc comment which looks like the following: /* @var $variable type */ . 该站点声明可以在PHP中使用此功能,方法是提供如下所示的PHPDoc注释: /* @var $variable type */ I am aware it concerns PHP, but I decided to give it a try in JavaScript as well: 我知道它涉及PHP,但我决定尝试使用JavaScript:

/* @var context CanvasRenderingContext2D */

Unfortunately, the above did not work. 不幸的是,上述方法无效。 I thought that maybe NetBeans for some reason doesn't know that type and tried something simpler: 我认为,由于某些原因,NetBeans可能不知道该类型并尝试了更简单的方法:

/* @var test HTMLImageElement */

Again, there were no proposals for test except for the ones that are native to all JavaScript objects. 同样,除了所有JavaScript对象的原生代码之外,没有任何test提议。

So, is this even possible? 那么,这甚至可能吗? I would appreciate it if someone provided a solution. 如果有人提供了解决方案,我将不胜感激。 Thanks in advance! 提前致谢!

JavaScript uses JSDoc, not PHPDoc, so you need to use JavaScript使用JSDoc,而不是PHPDoc,因此您需要使用

/** @type HTMLImageElement */
var myImage;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM