javascript jsHint “myFunction 已定义但从未使用过”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19763987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jsHint "myFunction is defined but never used"
提问by gespinha
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.
我正在用 JsHint 检查我的 javascript,它总是返回一个错误,说一个函数从未使用过。嗯,这并不完全正确。
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>
)将我的 javascript 集成到项目索引上,并且在索引上,我myJs.js
在实际文件链接之后调用文件上的函数。所以它看起来像这样:
<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.
我在实际文档中而不是在myJs.js
文件中调用我的函数的原因是因为我需要在其中运行一些 PHP 函数,并且我需要从我的索引文件中调用它们。
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?
这里的问题是 JsHint 返回了一个有害但令人讨厌的错误,说“myFunction 已定义但从未使用过”。它不知道我的函数以后可以在我的索引中调用吗?我该如何解决这个问题?我应该简单地忽略它吗?
回答by Karl-Johan Sj?gren
At the top of myJs.js
you could try adding an exported declarationwith your function name, something like this.
在顶部,myJs.js
您可以尝试使用您的函数名称添加导出的声明,就像这样。
/* exported myFunction */
This should silence your warning.
这应该使您的警告静音。