jslint给出了一个字符串,而是看到{a}

时间:2013-08-29 17:26:39

标签: javascript jslint

我有以下代码片段似乎是正确的,但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;
};

错误的一行是这样的,在"。#34;:     if(typeof this [currentType] === PRIMITIVE_TYPE.stringType){

错误的确切文字是:     期待一个字符串,而不是看到'。#。#。

toString()按预期执行。除了将表达式的右侧放入另一个变量之外,我无法看到应该更改以避免错误。该错误尚未在jslinterrors.com中描述。

2 个答案:

答案 0 :(得分:0)

正如@SLaks在评论中所述,JSLint会在遇到比较运算符时发出警告,其中一个操作数是typeof表达式而另一个操作数不是字符串文字。

以下是执行此检查的代码的缩减版本:

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
}

一旦我有机会,我会立即通过http://jslinterrors.com

答案 1 :(得分:0)

  

toString()按预期执行。

代码完全有效,所以是的。它可以。

请记住,jsLint不是在寻找错误;它正在寻找它认为不好的事情。

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

应将Lint错误视为指导,而不是严格遵守并导致构建失败的内容。

您可能还想考虑使用jsHint而不是jsLint。 jsHint基于jsLint,但对于它抱怨的内容往往更加务实。

希望有所帮助。