3

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!

1 Answer 1

5

JavaScript uses JSDoc, not PHPDoc, so you need to use

/** @type HTMLImageElement */
var myImage;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it works fine with HTMLImageElement, unfortunately there are no hints for CanvasRenderingContext2D. It seems they haven't added support for that yet: netbeans.org/bugzilla/show_bug.cgi?id=234149 but anyway this is the right answer to my question, so I'm accepting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.