Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jslint应提供一个字符串,而应看到{a}_Javascript_Jslint - Fatal编程技术网

Javascript jslint应提供一个字符串,而应看到{a}

Javascript jslint应提供一个字符串,而应看到{a},javascript,jslint,Javascript,Jslint,下面的代码片段看起来是正确的,但是jslint不喜欢它 var VALID_TYPE = { "stringType" : "string", "arrayType" : "array", "objectType" : "object" }, DEFAULT_FIRST = 1, DEFAULT_LAST = 1, PRIMITIVE_TYPE = { "stringType" : "string", "arrayType" : "array",

下面的代码片段看起来是正确的,但是jslint不喜欢它

var VALID_TYPE = {
    "stringType" : "string",
    "arrayType" : "array",
    "objectType" : "object"
},
    DEFAULT_FIRST = 1, DEFAULT_LAST = 1, PRIMITIVE_TYPE = {
    "stringType" : "string",
    "arrayType" : "array",
    "objectType" : "object",
    "undefinedType" : "undefined",
    "booleanType" : "boolean",
    "numberType" : "number"
};
VALID_TYPE.toString = function () {
    var types = [], currentType;
    for (currentType in this) {
        if (typeof this[currentType] === PRIMITIVE_TYPE.stringType) {
            types.push(this[currentType]);
        }
    }
    var outputString = types.join(', ');
    return outputString;
};
错误的行是这样的,在: 如果此[currentType]的类型===基本类型.stringType{

错误的确切文本为: 应为字符串,而应为“.”


toString按预期执行。除了将表达式的右侧放入另一个变量之外,我看不出应该更改什么来避免错误。错误尚未在jslinterrors.com上描述。

正如@SLaks在注释中所述,当遇到其中一个操作数为typ的比较运算符时,JSLint将发出警告eof表达式和其他操作数不是字符串文字

下面是执行此检查的代码的精简版本:

function relation(s, eqeq) {
    var x = infix(s, 100, function (left, that) {
        // ...
        if (are_similar(left, right) ||
                ((left.id === '(string)' || left.id === '(number)') &&
                (right.id === '(string)' || right.id === '(number)'))) {
            that.warn('weird_relation');
        } else if (left.id === 'typeof') {
            if (right.id !== '(string)') {
                right.warn("expected_string_a", artifact(right));
            } else if (right.string === 'undefined' || right.string === 'null') {
                left.warn("unexpected_typeof_a", right.string);
            }
        } else if (right.id === 'typeof') {
            if (left.id !== '(string)') {
                left.warn("expected_string_a", artifact(left));
            } else if (left.string === 'undefined' || left.string === 'null') {
                right.warn("unexpected_typeof_a", left.string);
            }
        }
        // ...
    });
    // ...
}
给出特定警告的唯一其他时间是当JSLint遇到无引号的JSON属性时:

{
    a: 1
}
我一有机会就会把这事提出来

toString按预期执行

代码完全有效,所以可以

请记住,jsLint并不是在寻找错误,而是在寻找它认为不好的做法

但这些事情并不总是在每种情况下都是绝对错误的;通常都有一个合法的用例,如果你有其中一个,那么你仍然会得到错误,但是你必须忽略它

Lint错误应该被视为一个指南,而不是需要严格遵守并导致构建失败的东西

您可能还想考虑使用JSHIT而不是JSLIN。JSHIT基于JSLIt,但它抱怨的倾向更为务实。


希望能有帮助。

当您将typeof与非常量进行比较时,JSLint似乎不喜欢它。不知道为什么。。。