gpt4 book ai didi

javascript - JavaScript 中的迭代

转载 作者:行者123 更新时间:2023-11-29 15:29:22 27 4
gpt4 key购买 nike

<分区>

This MDN page声明:

...you can iterate over an array using the following:

for (var i = 0; i < a.length; i++) {
// Do something with a[i]
}

This is slightly inefficient as you are looking up the length property once every loop. An improvement is this:

for (var i = 0, len = a.length; i < len; i++) {
// Do something with a[i]
}

A nicer-looking but limited idiom is:

for (var i = 0, item; item = a[i++];) {
// Do something with item
}

这些真的是普遍接受的迭代方式吗?现代 JavaScript 引擎不会简单地优化第一个吗?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com