简体   繁体   中英

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

I am using NetBeans, specifically the HTML5 & PHP bundle. Currently I'm working on a game which makes use of the HTML5 canvas element. I have a global variable called 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 . I would like NetBeans to provide code completion feature for context assuming it represents a specific type (namely, CanvasRenderingContext2D ). After doing some research I found this: 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 */ . I am aware it concerns PHP, but I decided to give it a try in JavaScript as well:

/* @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:

/* @var test HTMLImageElement */

Again, there were no proposals for test except for the ones that are native to all JavaScript objects.

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

/** @type HTMLImageElement */
var myImage;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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