久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 JavaScript 中執行循環的最佳方法是什么

What is the best way to do loops in JavaScript(在 JavaScript 中執行循環的最佳方法是什么)
本文介紹了在 JavaScript 中執行循環的最佳方法是什么的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我偶然發現了幾種JavaScript循環的方法,我最喜歡的是:

for(var i = 0; i < a.length; i++){變量元素 = a[i];}

但正如這里測試的那樣 (http://www.robertnyman.com/2008/04/11/javascript-loop-performance/),它可能應該被寫成只計算一次長度.

在 jQuery 中有一個 .each 可以粘貼一個函數.我更喜歡這個,因為我不必像上面的解決方案那樣輸入兩次數組.

如果 JavaScript 支持宏,那么你自己動手做就是小菜一碟,但遺憾的是它不支持.

那你們用什么?

解決方案

我已經開始在相關的地方使用迭代器.性能是合理的,但更重要的是它允許您封裝循環邏輯:

函數 createIterator(x) {變量 i = 0;返回函數(){返回 x[i++];};}

然后使用:

var iterator=createIterator(['a','b','c','d','e','f','g']);迭代器();

返回a";

迭代器();

返回b";

等等.

迭代整個列表并顯示每個項目:

<上一頁>無功電流;而(當前=迭代器()){控制臺.log(當前);}

請注意,上述內容僅適用于迭代包含非虛假"值的列表.如果此數組包含以下任何一項:

  • 0
  • 錯誤
  • "
  • NaN

上一個循環會停在那個項目上,并不總是你想要/期望的.

為了避免這種用法:

var 當前;而((當前=迭代器())!==未定義){控制臺.log(當前);}

I have stumbled into several methods of looping in JavaScript, what I like the most is:

for(var i = 0; i < a.length; i++){
    var element = a[i];
}

But as tested here (http://www.robertnyman.com/2008/04/11/javascript-loop-performance/), it should probably be written so that the length is only calculated once.

In jQuery there is a .each that you can stick a function. I like this a little better, because I don't have to type the array twice, like in the above solution.

If JavaScript supported macros it would be a piece of cake to roll your own, but sadly it does not.

So what do you guys use?

解決方案

I've started using iterators where relevant. Performance is reasonable, however more importantly it allows you to encapsulate the looping logic:

function createIterator(x) {
    var i = 0;

     return function(){
       return x[i++];
    };
}

Then to use:

var iterator=createIterator(['a','b','c','d','e','f','g']);

iterator();

returns "a";

iterator();

returns "b";

and so on.

To iterate the whole list and display each item:

var current;

while(current=iterator())
{
    console.log(current);
}

Be aware that the above is only acceptable for iterating a list that contains "non-falsy" values. If this array contained any of:

  • 0
  • false
  • ""
  • null
  • NaN

the previous loop would stop at that item, not always what you want/expect.

To avoid this use:

var current;

while((current=iterator())!==undefined)
{
   console.log(current);
}

這篇關于在 JavaScript 中執行循環的最佳方法是什么的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 人人干人人干人人 | 国产精品亚洲一区 | 日韩电影一区 | 久久成人午夜 | 91精品国产综合久久久久久漫画 | 成人在线观看网站 | 国产区在线观看 | 久久久久久久久久久久久91 | 91久久国产综合久久 | 日韩成人精品 | 日本久久黄色 | 国产最好的av国产大片 | 国产日韩一区二区 | 亚洲网站在线观看 | 91网站视频在线观看 | 成人在线观看欧美 | 国产区一区| 成人免费淫片aa视频免费 | 波多野结衣中文字幕一区二区三区 | 在线精品一区二区 | 成人午夜性成交 | a在线观看 | 国产精品成人在线 | 国产91亚洲精品 | 精品一区二区在线观看 | 欧美视频 亚洲视频 | 国产精品一区二区久久 | 日韩中文一区二区三区 | 久久久久国产精品 | 成人国产精品色哟哟 | 欧美日韩在线成人 | 日韩av手机在线观看 | 亚洲精品一区二区三区蜜桃久 | 欧美日韩18 | 中文字幕一页二页 | 日韩国产中文字幕 | 在线播放中文字幕 | a国产视频 | 九九免费 | 午夜日韩视频 | 中文字幕av中文字幕 |