How to use "Go to Declaration" with NetBeans and javascript?

3.6k views Asked by At

I know we can use Ctrl+Click or Ctrl+B in NetBeans, but it doesn’t work for me when I’m writing javascript files.

And I’m not the only one (sadly that question has no reply).

I can see the functions on the Navigator, but I can’t use “Go to declaration”.

I’m declaring my functions this way:

function anyName(params...) { ... }

I tried changing to this style:

var anyName = function (params...) { ... }

But that didn’t work either.

I’m using Netbeans 6.9.1.

More info:

  1. NetBeans supports “Go to declaration” in javascript.
  2. As I said, the function is recognized because I can see it in the Navigator.
  3. I can use Ctrl+O and then search for my function, and NetBeans can find it when I do that. I’m using this right now as a poor replacement for “Go to declaration”.
  4. I’ve noticed that I don’t have code completion either. Following the above example, if I write “an” (Ctrl+Space) I can see a lot of functions and methods but I can’t find my function (anyName).

I think I’m doing something really wrong, but I don’t know what.

2

There are 2 answers

0
Enrique On BEST ANSWER

The problem seems to be in defining everything as “global”. If you work in your own namespace — that is, create a global object and define everything there — then Netbeans can understand better where your code is and can also give you type hints.

2
Mike Christensen On

I think the short answer is NetBeans doesn't have a good parser for JavaScript. JS is such a loosely typed language, it could be incredibly difficult to "Go To" the actual definition of a function. Take these examples:

function callStuff(myFunc)
{
   myFunc(); //Where does this go?
}

callStuff(function () { window.alert(123); });

Or:

var x = {
  X: function () { },
  Y: function () { },
};

x.Z = function () { };

x.Y(); //Where do I go?
x.Z(); //How about this?

Or maybe:

string s = "window.alert(123);";
var callback = Function(s);
callback(); //Now we just made a function with a string, weird..

So as you can see, a JavaScript IDE would need to have an immense amount of knowledge on the run-time execution of your script to figure out exactly where a function was defined. There's a few IDEs that fake it pretty well if you use standard syntax or very obvious function declarations, but I've yet to see anything incredibly useful in this area. It's most likely not really something NetBeans has made an effort to support, since it's such a Java-centric IDE.