JavaScript中三種for循環(huán)語句的使用總結(jié)(for、for...in、for...of)
前言
每個接觸JS的開發(fā)人員都不可避免的與for循環(huán)打交道,畢竟這是遍歷必不可少的工具之一。JavaScript 中的 for 循環(huán)語句相信大家都已經(jīng)快用厭了,現(xiàn)在有好多文章都在講怎么減少代碼中的 for 循環(huán)語句,但是,你又不得不承認(rèn)它們真的很有用。今天,我來總結(jié)一下前端 JavaScript 中三種 for 循環(huán)語句。
for
這大概是應(yīng)用最廣的循環(huán)語句了吧,簡單實(shí)用,且大多數(shù)時候性能還是在線的,唯一的缺點(diǎn)大概就是太普通,沒有特色,導(dǎo)致很多人現(xiàn)在不愿用它。
const array = [4, 7, 9, 2, 6]; for (let index = 0; index < array.length; index++) { const element = array[index]; console.log(element); } // 4, 7, 9, 2, 6
for...in
for...in 語句可以以任意順序遍歷一個對象的除 Symbol 以外的可枚舉屬性。
const temp = {name: "temp"}; function Apple() { this.color = 'red'; } Apple.prototype = temp; const obj = new Apple(); for (const prop in obj) { console.log(`obj.${ prop } = ${ obj[prop] }`); } // obj.color = red // obj.name = temp
如果你只要考慮對象本身的屬性,而不是它的原型,那么使用 getOwnPropertyNames() 或執(zhí)行 hasOwnProperty() 來確定某屬性是否是對象本身的屬性。
const temp = {name: "temp"}; function Apple() { this.color = 'red'; } Apple.prototype = temp; const obj = new Apple(); for (const prop in obj) { if (obj.hasOwnProperty(prop)) { console.log(`obj.${ prop } = ${ obj[prop] }`); } } // obj.color = red
當(dāng)然,也可以用來遍歷數(shù)組。
const arr = [1, 2, 3, 4, 5]; for (const key in arr) { console.log(key) } // 0,1,2,3,4
使用 for...in 可以遍歷數(shù)組,但是會存在以下問題:
- index 索引為字符串型數(shù)字(注意,非數(shù)字),不能直接進(jìn)行幾何運(yùn)算。
- 遍歷順序有可能不是按照實(shí)際數(shù)組的內(nèi)部順序(可能按照隨機(jī)順序)。
所以一般不建議使用 for...in 來遍歷數(shù)組。
for...of
for...of 語句在可迭代對象(包括 Array,Map,Set,String,TypedArray,arguments 對象等等)上創(chuàng)建一個迭代循環(huán),調(diào)用自定義迭代鉤子,并為每個不同屬性的值執(zhí)行語句。
const array = ['a', 'b', 'c']; for (const element of array) { console.log(element); } // a // b // c
for...of 和 for...in 的區(qū)別:
- for...in 語句以任意順序迭代對象的可枚舉屬性。
- for...of 語句遍歷可迭代對象定義要迭代的數(shù)據(jù)。
Object.prototype.objCustom = function () { }; Array.prototype.arrCustom = function () { }; let iterable = [3, 5, 7]; iterable.foo = 'hello'; for (const key in iterable) { console.log(key); // logs 0, 1, 2, "foo", "arrCustom", "objCustom" } // 0, 1, 2, "foo", "arrCustom", "objCustom" for (const key of iterable) { console.log(key); } // 3, 5, 7
使用 for...of 遍歷 Map 結(jié)構(gòu):
let nodes = new Map(); nodes.set("node1", "t1") .set("node2", "t2") .set("node3", "t3"); for (const [node, content] of nodes) { console.log(node, content); } // node1 t1 // node2 t2 // node3 t3
可以看出,使用 for...of 遍歷 Map 結(jié)構(gòu)還是挺方便的,推薦使用!
總結(jié)
- 如果普通 for 循環(huán)用膩了,推薦使用 for...of 來替代。
- 這三種循環(huán)都可以使用 break 關(guān)鍵字來終止循環(huán),也可以使用 continue 關(guān)鍵字來跳過本次循環(huán)。
- for...of 循環(huán)的適用范圍最大。
到此這篇關(guān)于JavaScript中三種for循環(huán)語句使用總結(jié)的文章就介紹到這了,更多相關(guān)JS中for循環(huán)語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS實(shí)現(xiàn)針對給定時間的倒計(jì)時功能示例
這篇文章主要介紹了JS實(shí)現(xiàn)針對給定時間的倒計(jì)時功能,結(jié)合具體實(shí)例形式分析了javascript日期時間的正則判定與動態(tài)運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-04-04JavaScript實(shí)現(xiàn)重力下落與彈性效果的方法分析
這篇文章主要介紹了JavaScript實(shí)現(xiàn)重力下落與彈性效果的方法,結(jié)合實(shí)例形式分析了javascript重力下落及彈性效果的原理與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-12-12js實(shí)現(xiàn)圖片輪播效果學(xué)習(xí)筆記
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)圖片輪播效果的學(xué)習(xí)筆記,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07javascript:文字不間斷向左移動的實(shí)例代碼
這篇文章介紹了javascript:文字不間斷向左移動的實(shí)例代碼,有需要的朋友可以參考一下2013-08-08js實(shí)現(xiàn)購物網(wǎng)站放大鏡功能
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)購物網(wǎng)站放大鏡功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06