English 中文(简体)
使用 Javascamp 标签
原标题:Using labels in javascript
  • 时间:2012-05-28 14:11:53
  •  标签:
  • javascript

您能向我解释一下 如何在与断线互动的语句上使用标签吗? 切换,同时切换,同时切换,同时切换,同时切换,并用于。

请使用示例 。

问题回答

com/2007/03/09/the-javascript-goto-statement/"rel="nofollow" article

文章中的联署材料法典:

var pastures = getPastures();
 var i, pastureLen = pastures.length;

pastureLoop:
 for (i = 0; i < pastureLen; i++)
 {
    var pasture = pastures[i];
    var cows = pasture.getCows();

   var j, numCows = cows.length;
    for (j = 0; j < numCows; j++)
    {
       var cow = cows[j];
       if (cow.isEating())
          { continue pastureLoop; }
    }

   // No cows were eating, so fire the callback for pasture[i]
    pasture.executeCallback();    // or whatever
 }

查询Mozilla开发者网络 < a href="https:// developmenter.mozilla.org/en/JavaScript/Reference/ Statements/ label" rel="Nofollow noreferrer" >Language Reference :

提供带有标识符的语句,您可参考该语句使用中断或继续使用语句。

例如,您可以使用标签来识别循环,然后使用中断或继续语句来表示一个程序是中断循环还是继续执行循环。

请注意,他们也说:

JavaScript 使用标签并不常用 JavaScript, 因为这些标签使程序更难阅读理解。 尽可能避免使用标签,并视具体情况, 更喜欢调用功能或丢弃错误

不使用标签 。

示例:

// no label
while (condition) {
    // do something
}

label: ( 标签语句 ),基本上用于断开或继续断开或延续语句的语句,这些语句有助于断开或继续贴上标签的语句,与普通的 break 语句不同,这些语句只打破即时循环。

让我以演示语解释 break continue

断开 < 强 > :

let i, j;

loop1:
for (i = 0; i < 3; i++) {
  loop2:
  for (j = 0; j < 3; j++) {
    if (i === 1 && j === 1) {
        break loop1; // terminate the whole loop labelled as loop1 unline normal break which will only terminate the current loop)
    }
    console.log( i =   + i +  , j =   + j);
  }
}

继续<强>:

let str =   ;

loop1:
for (let i = 0; i < 5; i++) {
  if (i === 1) {
    continue loop1; // This statement will again run the loop labelled as loop1
  }
  str = str + i;
}

console.log(str); //  0234 




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签