解构里面再次解构_解构FOR循环

解构里面再次解构

We've all seen it often, and probably used it more often than we care to admit...

我们都经常看到它,并且使用它的频率可能比我们想承认的要高。

for(var i=0; i < something.length; i++)

Used - in some variation - in some of the most common web- and RIA-development languages (ActionScript, JavaScript, PHP), looping in general (and the "for" loop, specifically) is commonly used but uncommonly understood.

在某些最常用的Web和RIA开发语言(ActionScript,JavaScript,PHP)中使用(以某种变体形式),通常使用循环(尤其是“ for”循环),但是这种理解很少。

What is the exact semantic of the "for" loop? Understanding precisely how it works isn't necessarily intuitive, but it is enlightening.

“ for”循环的确切含义是什么? 准确了解它的工作方式不一定是直观的,但它是有启发性的。

Consider this:

考虑一下:

for(;;)

Ever seen it? Probably not. But it works, and I actually used exactly that in a real world application not long ago. Perhaps even more strangely, how about a for loop without following statements, like this (note the terminating semicolon):

见过吗? 可能不是。 但这确实有效,不久前我在实际的应用程序中实际使用了它。 也许更奇怪的是,不带以下语句的for循环怎么样(请注意终止分号):

for(var i = 0; i < 10; i++);

It's not entirely implausible:

这并非完全不可行:

var word="hallelujah";
for(var i = ""; i != word; i += word.charAt(i.length));
alert(i);

The above would iterate 10 times, but the alert would only display once - "hallelujah".  Let's examine each apparatus:

上面的代码将重复10次,但警报仅显示一次-“ hallelujah”。 让我们检查一下每种设备:

the first apparatus - PART A - instantiation - sets i to an empty string.

第一个设备-PART A-实例化-将i设置为空字符串。

the second apparatus - PART B - evaluation - instructs the loop to iterate until i equals the word variable.

第二个设备-B部分 -评估-指示循环进行迭代,直到i等于单词变量为止。

the third apparatus - PART C - iteration - appends to the i string the next character from the word string.

第三装置- C部 -迭代-追加到第i串从字符串的下一个字符。

The semicolon at the end of line 2 won't terminate the loop, but does indicate an absence of statements - so no actions occur until  line 3, the next executable statement: the alert.

第2行末尾的分号不会终止循环,但确实表明没有语句-因此,直到第3行(下一个可执行语句:警报)之前,不会发生任何操作。

The for loop is composed of 3 separate and distinct apparatus, separated by the semicolons: for( PART A ; PART B; PART C).

for循环由3个不同的设备组成,并用分号隔开:for( PART A ; PART B ; PART C )。

The first (Part A) is instantiation. You can declare and assign variables here, even execute single-line statements, or do nothing at all. If i has been declared earlier in the code, it doesn't need to be reinstantiated here. The following is fine:

第一个( A部分 )是实例化。 您可以在此处声明和分配变量,甚至执行单行语句,或者什么都不做。 如果已在代码的前面声明过,则无需在此处重新实例化。 很好:

var i=0;
for(; i < 10; i++)

As is

照原样

for(var i = 0, today = new Date(), isWeekend = (today.getDay() %  6) < 1; i < 10; i++)

The above would instantiate several variables:

上面将实例化几个变量:

i would be set to 0

将设置为0

today would be a new Date object

今天将是一个新的Date对象

isWeekend would be a boolean value from the Date object that was just created, invoking the getDay method and applying a modulus operation.  The return is true if the day was Saturday or Sunday, false otherwise.

isWeekend将是刚创建的Date对象的一个​​布尔值,调用getDay方法并应用模运算。 如果一天是星期六或星期日,则返回true,否则返回false。

All these variables would be set before the first iteration, and referenced within the same apparatus.  Each could be further tested or manipulated in the statement block.

所有这些变量将在第一次迭代之前设置,并在同一设备中引用。 每个语句都可以在语句块中进行进一步测试或操纵。

Anything before the first semi-colon functions exactly as if it were on a separate line of code.

第一个分号之前的所有内容都完全像在单独的代码行中一样起作用。

The second apparatus, Part B, is the conditional test, evaluated before each iteration.  As soon as the statement appearing here returns false (in any incarnation of "false", including 0, null or an empty string), the loop ends. This is limited to a single statement, but it can use several operators and comparitors. E.G.,

第二个设备B部分是条件测试,

for(var i = 0; i < 10 && (somevar != true || anothervar === false); i++)

the above loop would continue to increment the i variable until the following logic returned true:

上面的循环将继续增加i变量,直到以下逻辑返回true:

i was no longer less than 10 AND either somevar was not true OR anothervar was explicitly false.

不再小于10并且somevar不是true或anothervar是显式false。

Part B is independant of Part A.

B部分独立于A部分

for(; anyvar == false;)

The above loop would continue to iterate until, for whatever reason, anyvar evaluated to false.

上面的循环将继续进行迭代,直到由于某种原因而将anyvar评估为false为止。

Lets say a developer has a text area that he wants to fill with random text to determine scrolling or spacing behavior.  assume this text element has a reference of "textField"; he might use the following to populate it:

假设开发人员有一个文本区域,他想用随机文本填充该文本区域以确定滚动或间隔行为。 假定此文本元素具有对“ textField”的引用; 他可能会使用以下内容进行填充:

for(;textField.height < 1000;){
    textField.text += Math.random() + " ";
}

This could be even further shortened by ommitting the statement block entirely and incrementing the text property in the third apparatus, like so:

通过完全省略语句块并增加第三个设备中的text属性,可以进一步缩短此时间,如下所示:

for(;textField.height < 1000; textField.text += Math.random() + " ");

Note the semicolon at the end of line 1 - this loop is a complete line of code that will execute (or compile) properly.

注意第1行末尾的分号-此循环是完整的代码行,可以正确执行(或编译)。

The third apparatus, Part C, is the iterative statement - this statement occurs during every loop, at the end of the statement block. It need not be an incrementation (i++),

第三部分是C部分 ,它是迭代语句-该语句在每个循环中的语句块结尾处发生。 不必是增量(i ++),

for(var i = 0; i < 0.5; i = Math.random())

The above loop would set i to a random value between 0 and 1 until that random value evaluated to greater than 0.5.  That means it might happen once, or a dozen times, but likely would iterate once or twice.

上面的循环会将i设置为0到1之间的随机值,直到该随机值评估为大于0.5。 这意味着它可能发生一次或十几次,但可能会迭代一两次。

As with the other apparatuses, this apparatus is not required.  Consider the following:

与其他设备一样,不需要该设备。 考虑以下:

for(var i = 0; i < 10;)

This loop would have no built-in incrementation, and if the statement block did not either include a break statement (or a return statement if included within a function block), or assign a value to i that might exceed 10, it would result in infinite iterations.

该循环将没有内置的增量,并且如果语句块不包含break语句(或者如果包含在功能块中则不包含return语句),或者不给i赋值可能超过10,则将导致无限迭代。

Some of these examples may seem unlikely, but understanding how the mechanism works is an important step in understanding how to make the mechanism work how you want it to.

其中一些示例似乎不太可能,但是了解该机制的工作方式是了解如何使该机制按您希望的方式工作的重要一步。

function Test(){
	var attempts = [];
	for(;;){
		var attempt = Math.random();
		attempts.push(attempt);
		if(attempt > 0.5) return attempts;
	};
};
alert(Test());

Happy looping :)

快乐循环:)

翻译自: https://www.experts-exchange.com/articles/1937/Deconstructing-the-FOR-Loop.html

解构里面再次解构

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值