2012-08-29 42 views
0

可能重复:
jslint error: Unexpected 'in'. Compare with undefined, or use the hasOwnProperty为什么JSLint的抱怨这个代码,我应该怎么解决这个问题

为什么JSLint的抱怨这个代码,我应该怎么解决它。

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

显然是递归被定义。

+2

jslint抱怨什么?请包括警告。 –

+0

JSLint的投诉正是其中的原因:“意外”in。与undefined比较,或使用hasOwnProperty方法代替。“ – pimvdb

回答

1

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

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