【问题标题】:Why jslint complains about this code and how should I fix it [duplicate]为什么 jslint 抱怨此代码以及我应该如何修复它[重复]
【发布时间】:2012-08-29 13:42:59
【问题描述】:

可能重复:
jslint error: Unexpected 'in'. Compare with undefined, or use the hasOwnProperty

为什么 jslint 抱怨此代码以及我应该如何修复它。

            if ('children' in object) {
                for (key in object.children) {
                    recurse(object.children[key], key);
                }
            }

显然定义了递归。

【问题讨论】:

  • jslint 抱怨什么?请包括警告。
  • JSLint的抱怨正是原因:“Unexpected 'in'。与undefined比较,或者使用hasOwnProperty方法。”。

标签: javascript jslint


【解决方案1】:

您缺少一个变量。另外,您没有使用“hasOwnProperty”。

if (object.hasOwnProperty('children')) {
    for (var key in object.children) {
        if(object.children.hasOwnProperty(key)) {
            recurse(object.children[key], key);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2016-07-12
    • 2011-05-11
    • 1970-01-01
    相关资源
    最近更新 更多