我在我的JavaScript文件上使用了JSLint。它抛出错误:
for( ind in evtListeners ) {
第41行字符9的问题:for in的主体应该是 包装在if语句中以过滤不需要的 原型中的属性。
这是什么意思?
我在我的JavaScript文件上使用了JSLint。它抛出错误:
for( ind in evtListeners ) {
第41行字符9的问题:for in的主体应该是 包装在if语句中以过滤不需要的 原型中的属性。
这是什么意思?
当前回答
只是添加到for in/for/$的主题。每一个,我添加了一个jsperf测试用例使用$。每个vs for在:http://jsperf.com/each-vs-for-in/2
不同的浏览器/版本处理它的方式不同,但似乎$。每一种都是性能上最便宜的选择。
如果你使用for in来遍历一个关联数组/对象,知道你在寻找什么,忽略其他一切,使用$。每一个如果你使用jQuery,或者只是在(然后休息;一旦你到达了你所知道的最后一个元素)
如果你在一个数组中迭代每个键对来执行一些事情,如果你不使用jQuery,应该使用hasOwnProperty方法,并使用$。每一个如果你使用jQuery。
如果你不需要关联数组,请使用for(i=0;i<o.length;i++)。哈哈,chrome比for in或$.each快97%
其他回答
当然,这样说有点极端
...永远不要使用for in循环to 枚举数组。从来没有。使用 (var I = 0; 我< arr.length;我+ +)
?
道格拉斯·克罗克福德节选中的这部分值得强调
…第二种形式应该用with 对象……
如果你需要一个关联数组(又名哈希表/字典),其中键是命名的,而不是数字索引,你将不得不实现它作为一个对象,例如var myAssocArray = {key1: "value1", key2: "value2"…};
In this case myAssocArray.length will come up null (because this object doesn't have a 'length' property), and your i < myAssocArray.length won't get you very far. In addition to providing greater convenience, I would expect associative arrays to offer performance advantages in many situations, as the array keys can be useful properties (i.e. an array member's ID property or name), meaning you don't have to iterate through a lengthy array repeatedly evaluating if statements to find the array entry you're after.
无论如何,也感谢JSLint错误消息的解释,我将使用'isOwnProperty'检查现在通过我的无数关联数组交互!
瓦瓦的回答很中肯。如果使用jQuery,则使用$.each()函数处理这个问题,因此使用它更安全。
$.each(evtListeners, function(index, elem) {
// your code
});
@all——JavaScript中的所有东西都是一个对象(),所以像“只在对象上使用这个”这样的语句有点误导人。此外,JavaScript不是强类型的,所以1 == "1"是真的(尽管1 === "1"不是真的,Crockford在这方面做得很好)。当涉及到JS中数组的编程概念时,类型在定义中很重要。
@Brenton -没有必要成为术语独裁者;“关联数组”,“字典”,“哈希”,“对象”,这些编程概念都适用于JS中的一个结构。它是name (key, index)值对,其中值可以是任何其他对象(字符串也是对象)
所以, new Array()与[]相同
new Object()大致类似于{}
var myarray = [];
创建一个数组结构,其中所有索引(即键)必须为整数。它还允许通过.push()自动分配新索引
var myarray = ["one","two","three"];
确实最好通过(初始化;条件;更新){处理
但是:
var myarray = [];
myarray[100] = "foo";
myarray.push("bar");
试试这个:
var myarray = [], i;
myarray[100] = "foo";
myarray.push("bar");
myarray[150] = "baz";
myarray.push("qux");
alert(myarray.length);
for(i in myarray){
if(myarray.hasOwnProperty(i)){
alert(i+" : "+myarray[i]);
}
}
也许不是数组的最佳用法,但只是说明事情并不总是明确的。
如果你知道键值,而且肯定不是整数,你唯一的数组类结构选项就是对象。
var i, myarray= {
"first":"john",
"last":"doe",
100:"foo",
150:"baz"
};
for(i in myarray){
if(myarray.hasOwnProperty(i)){
alert(i+" : "+myarray[i]);
}
}
老实说,添加一整行只是为了检查键是否存在,而使用应该迭代对象的键的语法会导致..无用的。只需使用Object.keys(obj)即可。替换为forEach(key => {})
jslint的作者Douglas Crockford就这个问题写过(也说过)很多次。在他的网站的这一页上有一个部分是这样写的:
for Statement A for class of statements should have the following form: for (initialization; condition; update) { statements } for (variable in object) { if (filter) { statements } } The first form should be used with arrays and with loops of a predeterminable number of iterations. The second form should be used with objects. Be aware that members that are added to the prototype of the object will be included in the enumeration. It is wise to program defensively by using the hasOwnProperty method to distinguish the true members of the object: for (variable in object) { if (object.hasOwnProperty(variable)) { statements } }
Crockford还制作了一个关于YUI剧院的系列视频,在视频中他谈到了这一点。如果你对javascript有点兴趣,一定要看Crockford关于javascript的系列视频/演讲。