[英]Enhanced Javascript for-statement safe to use?
Using the jQuery for-loop is quite slow, which is the reason why I'm considering using the regular for-statement more often. 使用jQuery for循环非常慢,这就是为什么我考虑更频繁地使用常规for语句的原因。 In order to have direct access to the current element, I found the following syntax (for regular arrays, not for objects of course): 为了直接访问当前元素,我发现了以下语法(用于常规数组,当然不用于对象):
for (var i = 0, e; e = array[i]; i++) { ... }
where e
in the loop represents the current element. 循环中的e
表示当前元素。
Is this syntax safe to use across all browsers? 在所有浏览器中使用此语法是否安全?
Addition 加成
OK, I guess this could work, but it is not so useful anymore for a short notation: 好的,我想这可能有效,但是对于一个简短的符号来说,它不再那么有用了:
for (var i = 0, e; (e = array[i]) !== void(0); i++) { ... }
Thank you all for answering! 谢谢大家的回答!
That is not a very good loop. 那不是一个很好的循环。 Consider this array: 考虑以下数组:
var array = [0, 1, 2, 3];
It would stop on the first element because 0
is a falsey value. 它会在第一个元素上停止,因为0
是错误的值。 Same with 与相同
var array = ["foo", "bar", false, "hello"];
It would only get to "foo"
and "bar"
它只会变成"foo"
和"bar"
Consider using this loop 考虑使用此循环
for (var i=0, len=array.length; i<len; i++) { ... }
It works everywhere, only calculates array.length
once, and is plenty performant. 它在任何地方都有效,只计算一次array.length
,并且性能array.length
。
Per TJ's comment, the scope of the args i
and len
above will exist your current function. 根据TJ的评论,上面的args i
和len
的范围将存在您当前的功能。 So be careful that you don't make variable conflicts. 因此,请注意不要造成变量冲突。
A somewhat common (but clunky) way of safeguarding against this is prefixing vars with _
. 防止这种情况的一种常见(但笨拙)的方法是在vars前面加上_
。 Like this 像这样
for (var _i=0, _len=array.length; _i<_len; _i++) { ... }
As naomik points out , that form of loop will break if any of the array elements has a falsey value. 正如naomik指出的 ,如果任何数组元素具有falsey值,则这种循环形式将中断。 Falsey values are false
, null
, undefined
, ""
, 0
, and NaN
. Falsey值为false
, null
, undefined
, ""
, 0
和NaN
。 So it would work, for instance, for an array of non-null object references. 因此,例如,它适用于非空对象引用的数组。 Not so much for an array of strings or numbers. 对于字符串或数字数组,则不需要太多。
But if your question is about the syntax , then yes, it's "safe" in that it will work (and fail on falsey elements) in all JavaScript engines. 但是,如果您的问题是关于语法的 ,那么是的,它是“安全的”,因为它将在所有JavaScript引擎中都可以工作(并且在false元素上失败)。 The key bits you're relying on are: 您所依赖的关键要素是:
That accessing the element beyond the end of the array (eg, at array[array.length]
) will give you a falsey value ( undefined
) rather than throwing an exception, and 访问超出数组末尾的元素(例如,在array[array.length]
)将为您提供一个false值( undefined
),而不是引发异常,并且
That the result of an assignment expression ( e = array[i]
) is the value that was assigned. 赋值表达式( e = array[i]
)的结果就是所赋值。
Yes, both of those are reliable. 是的,两者都是可靠的。 (Well, #1 is reliable if the array really is a JavaScript array. Host-provided array-like objects may vary.) (如果数组确实是JavaScript数组,则#1是可靠的。由主机提供的类似数组的对象可能会有所不同。)
In any case, note that neither i
nor e
is scoped only to the loop. 在任何情况下,请注意, i
和e
都不仅限于循环。 ES6 will have let
, which will be, but variables declared with var
are scoped to the function they're in. ES6将具有let
,但将使用var
声明的变量的作用域限于它们所在的函数。
I do not recommend that. 我不建议这样做。 You see, if your array looks like this for example: 您会看到,如果您的数组看起来像这样:
array = ["lala", 078, false, 992, "kas"];
Then your loop would only go through the first two, since the term e = array[i];
然后,由于条件e = array[i];
,您的循环将只经过前两个e = array[i];
would return false, because the third entry in the array is literally false. 将返回false,因为数组中的第三个条目实际上是false。 This is better: 这个更好:
for (var i = 0, e; (e = array[i])===undefined; i++) { ... }
Make sure no one overwrites the undefined variable, eg by using a closure: How does this JavaScript/JQuery Syntax work: (function( window, undefined ) { })(window)? 确保没有人覆盖未定义的变量,例如通过使用闭包: 此JavaScript / JQuery语法如何工作:(function(window,undefined){})(window)?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.