我有以下代码片段,看起来是正确的,但 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;
};
错误的行是这样的,在“.”处: if (typeof this[currentType] === PRIMITIVE_TYPE.stringType) {
错误的确切文本是: 预期是一个字符串,但看到的是“.”。
toString() 按预期执行。除了将表达式的右侧放入另一个变量之外,我看不出应该更改什么来避免错误。 jslinterrors.com 尚未描述该错误。
最佳答案
正如 @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 获取此信息一有机会我就尽快。
关于javascript - jslint 给出了预期的字符串,但看到了 {a},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18517356/