本文介绍了请帮我理解“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所说的:

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:

从概念上讲,有两种类型的表达式:为变量赋值的那些,以及那些只是有价值。例如,表达式 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.

如果你感到勇敢,您还可以查看,特别是以下部分:

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

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

07-29 17:17
查看更多