2012-08-29 41 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); 
     } 
    } 
}