IntelliJ IDEA中js代码报如下警告:
Possible iteration over unexpected (custom / inherited) members, probably missing hasOwnProperty check
Checks for any instances of unfiltered for-in loops in JavaScript. The use of this construct results in processing inherited or unexpected properties. You need to filter own properties with hasOwnProperty() method. The validation works in JavaScript, html or jsp files.
报警告的代码如下:
//后面的就是个循环对象
for(i in widgetArr){
if(widgetArr[i] != null){
updateWidgetMenu(widgetArr[i], id, M0DULE_TYPE_DASHBOARD, isEditDashboard);
}
}
修改后如下:
for(var i in widgetArr){
if(widgetArr.hasOwnProperty(i)){
if(widgetArr[i] != null){
updateWidgetMenu(widgetArr[i], id, M0DULE_TYPE_DASHBOARD, isEditDashboard);
}
}
}
区别在于:widgetArr. hasOwnProperty(i)
做了个这个判断。
JS for-in循环优化
本文介绍IntelliJ IDEA中JavaScript代码使用for-in循环时可能遇到的警告:未检查自有属性导致迭代了意外成员。通过使用hasOwnProperty方法进行过滤,可以避免这一问题。
1297

被折叠的 条评论
为什么被折叠?



