JavaScript循环:用于

Loops are programming’s infinite playlist: a way of repeating or iterating through something over and over until a particular condition is met. JavaScript has supported the classical for, while, do-while and for...in loops since its inception, with recent innovations in ECMAScript 5 & 6 (the latest JavaScript standards) adding even more.

循环是编程的无限播放列表:一种反复重复或迭代某些事物直到满足特定条件的方式。 自创建以来,JavaScript就一直支持经典的forwhiledo-whilefor...in循环,而ECMAScript 5和6(最新JavaScript标准)的最新创新功能甚至更多。

Loops need to be handled with care, as they constitute one of the few ways it is possible to place JavaScript into an infinitely recursive state, eventually consuming the computer’s processor and hanging the browser.

循环必须谨慎处理,因为它们构成了将JavaScript置于无限递归状态的少数几种方法之一,最终会消耗计算机的处理器并挂起浏览器。

对于 (for)

for is the most common loop structure in JavaScript coding. It takes an initialization, a tested condition (which if evaluated true keeps the loop running) and the next step for the loop. Given an array called browncoats:

for是JavaScript编码中最常见的循环结构。 它需要一个初始化,一个经过测试的条件(如果评估为true ,则将保持循环运行)以及循环的下一步。 给定一个名为browncoats数组

var browncoats =
	[ 'Malcom Reynolds', 'Zoe Washburne', 'Tracey Smith' ];

We can iterate through each “slot” in the array and report its data to the console:

我们可以遍历数组中的每个“插槽”,并将其数据报告给控制台:

for (var i = 0; i < browncoats.length; i++) {
	console.log(browncoats[i]);
}

In the past I had a hard time recalling the order of expressions for creating loop. I invented an acronym that was helpful to me, ICU: initial state, condition (to keep running) and “up” (incrementor). ICD is also possible, i.e. where the step decrements during the loop, counting down to 0.

过去,我很难回忆起创建循环的表达式顺序。 我发明了一个对我有用的首字母缩写词,ICU:初始状态,状态(保持运行)和“上升”(增量器)。 ICD也是可能的,即在循环过程中步长递减 ,递减至0。

I would also forget which expressions in the loop needed semi-colons. Just keep in mind that the semicolons go between the expressions, not before or after.

我也会忘记循环中的哪些表达式需要分号。 请记住,分号位于表达式之间 ,而不是在表达式之前或之后。

The result of the statement inside the loop:

循环内语句的结果:

Malcom Reynolds
Zoe Washburne
Tracey Smith

It’s important to note that the for loop does not constitute a new scope. This may cause the final value of i to be shared with the rest of your code, which is why it is explicitly set to 0 during initialization. Due to that fact, the following code would accomplish the same result as the example above:

重要的是要注意, for循环不会构成新的作用域 。 这可能会导致i的最终值与其余代码共享,这就是为什么在初始化期间将其显式设置为0原因。 因此,以下代码将完成与上面的示例相同的结果:

var i = 0;
for (; i < browncoats.length; i++) {
	console.log(browncoats[i]);
}

(Note that the semi-colon after the missing initialization expression is kept in place).

(请注意,缺少初始化表达式的分号将保留在原处)。

This is also partly the reason why you’ll often see different variable names, such as j used in sequential loops.

这也是部分原因,您经常会看到不同的变量名的原因,例如顺序循环中使用的j

Technically, if the loop only contains a single statement, it could all be placed on one line, without any need for curly braces. However, as code has a tendency to grow, it’s usually best to contain loop statements inside a block, even if they only start off as a single statement.

从技术上讲,如果循环仅包含一个语句,则可以将其全部放在一行上,而无需花括号。 但是,由于代码倾向于增长,因此通常最好将循环语句包含在一个块内,即使它们只是以单个语句开始。

It’s also possible to leave out the condition block, so long as the loop still has an “escape clause” inside the statement block in the form of a break:

只要循环在语句块内仍以break形式存在“转义子句”,也可以省略条件块:

for (var i = 0; ;i++) {
if (i >= browncoats.length) break;
	console.log(browncoats[i]);
}

Note the changed test for the condition, and that it goes before any other action taken in the block. break can be a useful test, immediately bringing the loop to a halt and allowing the rest of your script to run.

请注意已更改的条件测试,该测试在该块中执行的任何其他操作之前进行break可能是一个有用的测试,可以立即使循环暂停并允许其余脚本运行。

有无限远吗? (Got Infinity?)

Creating an accidental infinite loop is one of the “learning moments” of programming. If you cause a script to be stuck in an endless loop, pressing Command-/ or F8 will get you out of it.

创建意外的无限循环是编程的“学习时刻”之一。 如果您使脚本陷入无限循环,则按Command- /F8将使您摆脱困境。

New coders will sometimes deliberately attempt to create an infinite loop using for, on the basis of the “I want to build a rocketship, but all I have is this hammer” principle. Don’t do that: actual “infinite” loops are usually a job for setInterval, which I will discuss later.

新的编码人员有时会根据“我想制造一艘飞船,但我所拥有的只是锤子”的原理,故意使用for来创建一个无限循环。 不要这样做:实际的“无限”循环通常是setInterval的工作,我将在后面讨论。

翻译自: https://thenewcode.com/935/JavaScript-Loops-for

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值