JavaScript for loop syntax for array

I have seen the following style of writing a for loop in JavaScript a number of times:

for(var i=0, n=myArray.length; i<n; i++) {
// do something
}

what is the advantage of that over the following?

for(var i=0; i<myArray.length; i++) {
// do something
}

In theory, it only has to look up the length of myArray once, instead of each time it goes around the loop.

In practise, these days I believe browsers have JS engines smart enough to optimise the performance impact of that look up away.

链接地址: http://www.djcxy.com/p/70014.html

上一篇: Javascript为什么FOR IN是不好的做法?

下一篇: 用于数组的循环语法的JavaScript