JavaScript:JSLint错误“for的主体应该包含在if语句中以从原型中过滤掉不需要的属性” [英] JavaScript: JSLint error "The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype"

查看:1040
本文介绍了JavaScript:JSLint错误“for的主体应该包含在if语句中以从原型中过滤掉不需要的属性”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JSLint 工具来确保我的JavaScript严格。

I'm using the JSLint tool to ensure my JavaScript is "strict".

我收到以下错误但不明白如何修复它:

I'm receiving the following error but don't understand how to fix it:

The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype

For以下代码:

for (var i in keypairs) {
    ...
}

任何人都有任何想法如何修复它的JavaScript严格并且不会被JSLint标记

Anyone have any ideas how to fix this to that it's JavaScript "strict" and won't be flagged by JSLint

推荐答案

如果密钥对是一个数组,那么你应该真的迭代元素如:

If keypairs is an array, then you should really iterate over the elements like:

for(var i = 0; i < keypairs.length; i++) {
  ...
}

如果密钥对是一个哈希,然后JSLint正确地建议你检查你是否在运行t他适当的密钥类型(即,确认哈希是预期的类型)

If keypairs is a hash, then JSLint is correctly recommending that you check that you are operating on the appropriate key type (i.e., confirming that the hash is the expected type)

所以类似

for(var i in keypairs) {
  if(keypairs.hasOwnProperty(i)) {
    ...
  }
}

其中if验证了什么标准,确保你没有访问原型函数等。

where the if is validating whatever criteria ensures that you are not accessing a prototype function etc.

这篇关于JavaScript:JSLint错误“for的主体应该包含在if语句中以从原型中过滤掉不需要的属性”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆