繁体   English   中英

JavaScript JSHint-定义了“关闭”但从未使用过

[英]JavaScript JSHint - 'close' is defined but never used

我不知道为什么,但是我用作函数名称的某些单词出现了被定义但从未使用过的错误信息。

例如,下面的代码返回错误:

// I am using AngularJS
$scope.close = close;

function close() {
    /* Code here */
}

但这不是:

// I am using AngularJS
$scope.close2 = close2;

function close2() {
    /* Code here */
}

错误是在行function close() 为什么这么特别的名字? 如何消除此错误?

(注意:答案已经过大量编辑)

摘要

John Papa说在latedef中使用latedef并且至少隐式地忽略了JSLint问题。 在这里定义 latedef 。)

我认为,这里有一个很好玩的解决方案(请参见下文),其中包括Papa建议的样式的优点,以及在JSHint JSLint中插入的代码。

close2抱怨抱怨close2是“错误的”。 JSLint完全按照您的期望进行捕获。


close但不close2close2问题

对于它的价值,如果你粘贴代码(JSLint的格式化Pasteee既密切与close2 这里 )到JSLint.com双方 closeclose2导致错误。 如果您没有看到close2错误, close2我想这是JSHint的问题,但是确切地了解通过JSHint(在上下文中)了解的内容会更有用。

如此close并不是JSLint的特殊名称。 我想在上下文中查看您的“实际”代码,看看JSLint是否会说类似的话

只是要清楚,这在JSLint.com中断了

/*jslint sloppy:true, white:true */
/*global $scope */

$scope.close2 = close2;

function close2() {
    return "something";
}

产生'close2' was used before it was defined. $scope.close2 = close2; 'close2' was used before it was defined. $scope.close2 = close2;

我相信,如果您想知道JSHint为什么会中断,我们可以进行JSHint代码开发,但是至少要回答您的JS L int标记,您所看到的行为并没有发生。


怎么修

可以在您正在此处讨论的内容中看到此SO答案John Papa表示要在latedef中使用latedef 解决掉毛问题的一种方法是先忽略Papa并定义函数 ,但是,正如您在评论中提到的那样 ,这并不理想。

所以这是我能想到的最好的折衷方案...

  1. 声明但不定义将包含函数的变量。
  2. 插入您的Angular指令
  3. 从1定义功能。

绝对可以使JSHint错误静音,因为导致该错误的代码不再存在。 如果我正在做Angular并且需要遵循Papa风格,那就是我为保持Crockford的祝福而要做的事情。

例:

(function () {
    'use strict';
    // 1. Declare your function names. Minimally spammy!
    var theController;

    // 2. Directive
    angular
        .module('myApp')
        .controller('myAppCtrl', theController);

    // 3. *Define* the functions. No `latedef` needed, and JSLint compliant.
    // Keeps "the list of calls at the top of the page" and allows you to
    // "jump to each definition if you need more details". QED? ;^)
    theController = function () {
        return "so jslint doesn't complain about empty blocks";
    };
}());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM