分号在“for”开始时如何工作? [英] How does the semicolon work at the beginning of "for"?

查看:134
本文介绍了分号在“for”开始时如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

($ lt; $ k $)
{
if(k in t&& t [k] === searchElement)
return k;



$ b $ p
$ b

分号在循环开始时如何工作?



完整的代码在这里
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/IndexOf

解决方案

<第一部分是用来初始化变量的 initial-expression (参见 for construct ):

  -expression]; [condition]; [final-expression])
语句

括号在这种情况下意味着它是可选的。所以你不需要写任何初始化表达式,如果你没有任何变量来初始化。就像在这种情况下 k for 循环之前被初始化:

  var k = n> = 0 
? n
:Math.max(len - Math.abs(n),0); $;

for(; k {
if(k in t& t [k] === searchElement)
return K表;

$ / code>

你也可以把它写成 initial-expression 部分,但这不会是可读的:

  for(var k = n> = 0?n:Math.max (len  -  Math.abs(n),0); k  {
if(k in t& t [k] === searchElement)
返回k;
}


I just came across this code on the mozilla site and to me it looks broken but im sure its more I am just not familiar with its use

for (; k < len; k++)
    {
      if (k in t && t[k] === searchElement)
        return k;
    }

How does the semicolon work at the beginning of the loop?

full code is here https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/IndexOf

解决方案

The first part is the initial-expression that is used to initialize variables (see for construct):

 for ([initial-expression]; [condition]; [final-expression])
    statement

The brackets mean in this case that it’s optional. So you don’t need to write any initializer expression if you don’t have any variables to initialize. Like in this case where k is initialized before the for loop:

var k = n >= 0
      ? n
      : Math.max(len - Math.abs(n), 0);

for (; k < len; k++)
{
  if (k in t && t[k] === searchElement)
    return k;
}

You could also write it as initial-expression part but that wouldn’t be that readable:

for (var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0); k < len; k++)
{
  if (k in t && t[k] === searchElement)
    return k;
}

这篇关于分号在“for”开始时如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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