【问题标题】:jshint no error for forin (hasOwnProperty)jshint 没有错误 forin (hasOwnProperty)
【发布时间】:2013-05-29 08:24:07
【问题描述】:

为什么 jshint 没有报告以下代码的 forin (hasOwnProperty) 错误? jslint 会报告错误,但 jshint 不会。

/*jshint forin: true */

(function () {
    "use strict";

    var obj = {a: 1, b: 2}, i = null;

    for (i in obj) {
        if (i === 0) {
            console.log('blah...');
        }
    }
}());

【问题讨论】:

    标签: jslint jshint hasownproperty


    【解决方案1】:

    这是来自 JSHint 的相关 sn-p 代码(对格式稍作修改):

    if (
        state.option.forin && 
        s && 
        (s.length > 1 || typeof s[0] !== "object" || s[0].value !== "if")
    ) {
        warning("W089", this);
    }
    

    重要的部分是s[0].value !== "if"。如果 for...in 主体的第一条语句是 if 语句,无论该语句的条件如何,JSHint 都不会引发错误。

    【讨论】:

    • 哦,我明白了!这不是错误,这是设计使然。非常感谢您的回答:)
    • 好电话。为了完善 JSLint 确实 工作的原因,JSLint's current codefor 之后对 hasOwnProperty 进行了显式检查(而不是 JSHint 仅检查 if)在从第 3687 行开始的 case 块。
    猜你喜欢
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 2013-09-07
    • 2015-04-04
    • 2019-01-29
    相关资源
    最近更新 更多