在我的几个javascript文件上运行jshint时,我收到如下警告:
file.js: line X, col 93, 'fromParams' is defined but never used.
file.js: line X, col 72, 'toParams' is defined but never used.
file.js: line X, col 63, 'toState' is defined but never used.
file.js: line X, col 56, 'event' is defined but never used.
Run Code Online (Sandbox Code Playgroud)
对于这样的事情:
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
// ... some code that doesn't use event, toState, toParams, or fromParams...
});
Run Code Online (Sandbox Code Playgroud)
这经常出现在一种或另一种回调中 - 回调函数需要一定数量的参数,但我在函数中的代码并没有使用所有参数,所以jshint抱怨它们.但参数需要在那里!
应该有一些方法可以在代码的某些部分禁用此警告,如下所示:
/*jshint -W098 */
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
/*jshint +W098 */
Run Code Online (Sandbox Code Playgroud)
但由于jshint中的错误,它无效,请参阅此未解决的问题.
也可以对整个函数禁用此警告,如下所示:
/* jshint unused:false */
Run Code Online (Sandbox Code Playgroud)
...但这是不可接受的,因为它会抑制函数中所有未使用的变量的警告,并且我希望收到任何未使用的通知,除了我明确知道我不会使用的函数参数.
反正我是否可以解决这个问题?我非常希望我的代码不会触发任何linter警告,但就目前而言,jshint将报告几个"已定义但从未使用过"的警告,我不知道如何解决.