请帮我理解“while”循环使用此JavaScript代码段 [英] Please help me understand the "while" loop in this JavaScript snippet

查看:96
本文介绍了请帮我理解“while”循环使用此JavaScript代码段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这样一个片段用于在JavaScript中使用条件注释检测IE。

I have seen a snippet like this for detecting IE in JavaScript using conditional comments.

var ie = (function(){

    var undef, v = 3, div = document.createElement('div');

    // the while loop is used without an associated block: {}
    // so, only the condition within the () is executed.

    // semicolons arent allowed within the condition,
    //   so a comma is used to stand in for one
    // basically allowing the two separate statements 
    //   to be evaluated sequentially.

    while (
        div.innerHTML = '<!--[if gt IE '+(++v)+']><i></i><![endif]-->',
        div.getElementsByTagName('i')[0]
    );

    // each time it's evaluated, v gets incremented and
    //   tossed into the DOM as a conditional comment
    // the i element is then a child of the div.

    // the return value of the getEBTN call is used as 
    //   the final condition expression
    // if there is an i element (the IE conditional
    //   succeeded), then getEBTN's return is truthy
    // and the loop continues until there is no 
    //   more i elements.

    // In other words:  ** MAGIC**

    return v > 4 ? v : undef;

}());

上面给出的是保存爱尔兰的文件(并略有改进) 片段。我发布评论版本是为了让你知道我可能需要一个更简单的解释,如果有人可以。

The above given is the documented (and slightly improved) version by Paul Irish on a snippet by James Padolsey. I am posting the commented version to let you know that I might need a simpler explanation if anybody may.

我真的很想知道内部会发生什么环即可。我不明白。

I would really like to know what happens inside the while loop. I don't understand that.

推荐答案

(假设我没有笨拙地搞砸了)循环等效于以下内容:

(Assuming I haven't bungled this up horribly) the while loop is equivalent to the following:

var elt;

do
{
    v++;
    div.innerHTML = '<!--[if gt IE ' + v + ']><i></i><![endif]-->'
    elt = div.getElementsByTagName('i')[0];
} (while elt);








执行mdc或任何好的都覆盖了这个(stmt1,stmt2)的东西。

does mdc or any good ones cover this while(stmt1, stmt2) thing.

这是MDC所说的 while

Here's what MDC says about while:


while (condition)
    statement

条件

在每次循环之前计算的表达式。如果此条件的计算结果为 true ,则执行语句。当条件求值为 false 时,继续执行 while 循环后的语句。

condition
An expression evaluated before each pass through the loop. If this condition evaluates to true, statement is executed. When condition evaluates to false, execution continues with the statement after the while loop.

我们可以准确找出 表达式 是来自MDC的JavaScript:

We can find out exactly what an expression is in JavaScript from MDC:


表达式是评估为单个值的任何有效的文字,变量,运算符和表达式集合;值可以是数字,字符串或逻辑值。

An expression is any valid set of literals, variables, operators, and expressions that evaluates to a single value; the value can be a number, a string, or a logical value.

从概念上讲,有两种类型的表达式:为变量赋值的那些,以及那些只是有价值。例如,表达式 x = 7 是一个表达式,它将x赋值为7。该表达式本身的计算结果为7。此类表达式使用赋值运算符。另一方面,表达式 3 + 4 简单地计算为7;它不执行任务。这些表达式中使用的运算符简称为运算符

Conceptually, there are two types of expressions: those that assign a value to a variable, and those that simply have a value. For example, the expression x = 7 is an expression that assigns x the value seven. This expression itself evaluates to seven. Such expressions use assignment operators. On the other hand, the expression 3 + 4 simply evaluates to seven; it does not perform an assignment. The operators used in such expressions are referred to simply as operators.

如果你感到勇敢,您还可以查看 ECMA-262语言规范,特别是以下部分:

If you're feeling brave, you can also check out the ECMA-262 language specification, specifically the following sections:


  • 11 表达式,尤其是 11.14 逗号运算符(

  • 12.6.2 声明

  • 11 Expressions, especially 11.14 Comma Operator ( , )
  • 12.6.2 The while Statement

对不起,我无法提供直接链接,因为它全部在PDF内。

这篇关于请帮我理解“while”循环使用此JavaScript代码段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆