19

I am reviewing my javascript with JsHint, and it always returns an error, saying that a function is never used. Well, this is not entirely true.

I am integrating my javascript on the project index by external link (<script src="myJs.js"></script>), and on the index I call the functions I have on the myJs.js file after the actual file link. So it all looks like this:

<script src="myJs.js"></script>
<script>
myFunction();
</script>

The reason why I call my functions on the actual document and not in the myJs.js file is because I need some PHP functions running in it, and I need to call them from my index file.

The problem here is that JsHint returns a harmful, but annoying error, saying that "myFunction is defined but never used". Is it not aware that my function can later be called within my index? How can I solve this? Should I simply ignore it?

gespinha
  • 7,968
  • 16
  • 57
  • 91
  • Tools like jshint should _support_ you in making your decisions, not _make_ them for you. If you already _know_ that an error message only arises due to how you have organized your scripts, then yes, the smart thing to do would be to ignore it. (_Really_ smart would be coming to that conclusion without having to ask, and figure out that you don’t have to _listen_ to automatic tools “no matter what” by yourself.) – CBroe Nov 04 '13 at 08:47
  • 3
    Ignoring an error is never a good idea even if you know that it's an invalid one. Better to look to the JSHint documentation and see if you can somehow tell it that you know what you are doing in this specific case. If you just ignore it, how long will it take until you have 10-15 errors always showing up that you "know are working" and then start missing real ones? – Karl-Johan Sjögren Nov 04 '13 at 09:01
  • @Karl-JohanSjögren JSHint documentation on this kind of errors is not very detailed, so it doesn't help at all, aside from explaining the origin of the error (not exactly what I'm looking for). – gespinha Nov 04 '13 at 10:24
  • possible duplicate of [Prevent JSHint warning that 'functionName is defined but never used'](http://stackoverflow.com/questions/12607289/prevent-jshint-warning-that-functionname-is-defined-but-never-used) – ivarni Nov 11 '14 at 11:40

1 Answers1

30

At the top of myJs.js you could try adding an exported declaration with your function name, something like this.

/* exported myFunction */

This should silence your warning.

Karl-Johan Sjögren
  • 16,544
  • 7
  • 59
  • 68