JS中的for...of循環(huán)是什么

這篇文章給大家分享的是有關(guān)JS中的for...of循環(huán)是什么的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。

在孝南等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需搭建網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都全網(wǎng)營(yíng)銷,成都外貿(mào)網(wǎng)站建設(shè)公司,孝南網(wǎng)站建設(shè)費(fèi)用合理。

for...of語(yǔ)句創(chuàng)建的循環(huán)可以遍歷對(duì)象。在ES6中引入的for...of可以替代另外兩種循環(huán)語(yǔ)句for...in和forEach(),而且這個(gè)新的循環(huán)語(yǔ)句支持新的迭代協(xié)議。for...of允許你遍歷可迭代的數(shù)據(jù)結(jié)構(gòu),比如數(shù)組、字符串、映射、集合等。

語(yǔ)法

for (variable of iterable) {
    statement
}
  • variable:每個(gè)迭代的屬性值被分配給variable

  • iterable:一個(gè)具有可枚舉屬性并且可以迭代的對(duì)象

我們使用一些示例來(lái)闡述。

Arrays

array是簡(jiǎn)單的列表,看上去像object。數(shù)組原型有多種方法,允許在其上執(zhí)行操作,比如遍歷。下面的示例使用for...of來(lái)對(duì)一個(gè)array進(jìn)行遍歷操作:

const iterable = ['mini', 'mani', 'mo'];

for (const value of iterable) {
    console.log(value);
}

// Output:
// => mini
// => mani
// => mo

其結(jié)果就是打印出iterable數(shù)組中的每一個(gè)值。

Map

Map對(duì)象持有key-value對(duì)。對(duì)象和原始值可以當(dāng)作一個(gè)keyvalueMap對(duì)象根據(jù)插入的方式遍歷元素。換句話說(shuō),for...of在每次迭代中返回一個(gè)kay-value對(duì)的數(shù)組。

const iterable = new Map([['one', 1], ['two', 2]]);

for (const [key, value] of iterable) {
    console.log(`Key: ${key} and Value: ${value}`);
}

// Output:
// => Key: one and Value: 1
// => Key: two and Value: 2

Set

Set對(duì)象允許你存儲(chǔ)任何類型的唯一值,這些值可以是原始值或?qū)ο蟆?code>Set對(duì)象只是值的集合。Set元素的迭代是基于插入順序,每個(gè)值只能發(fā)生一次。如果你創(chuàng)建一個(gè)具有相同元素不止一次的Set,那么它仍然被認(rèn)為是單個(gè)元素。

const iterable = new Set([1, 1, 2, 2, 1]);

for (const value of iterable) {
    console.log(value);
}

// Output:
// => 1
// => 2

盡管我們創(chuàng)建的Set有多個(gè)12,但遍歷輸出的只有12。

String

字符串用于以文本形式存儲(chǔ)數(shù)據(jù)。

const iterable = 'javascript';

for (const value of iterable) {
    console.log(value);
}

// Output:
// => "j"
// => "a"
// => "v"
// => "a"
// => "s"
// => "c"
// => "r"
// => "i"
// => "p"
// => "t"

在這里,對(duì)字符串執(zhí)行迭代,并打印出每個(gè)索引上(index)的字符。

Arguments Object

把一個(gè)參數(shù)對(duì)象看作是一個(gè)類似數(shù)組的對(duì)象,與傳遞給函數(shù)的參數(shù)相對(duì)應(yīng)。這是一個(gè)用例:

function args() {
    for (const arg of arguments) {
        console.log(arg);
    }
}

args('a', 'b', 'c');

// Output:
// => a
// => b
// => c

你可能在想,到底發(fā)生了什么?正如我前面說(shuō)過(guò)的,當(dāng)調(diào)用函數(shù)時(shí),參數(shù)會(huì)接收傳入args()函數(shù)的任何參數(shù)。因此,如果我們將20個(gè)參數(shù)傳遞給args()函數(shù),我們將輸出20個(gè)參數(shù)。

在上面的示例基礎(chǔ)上做一些調(diào)整,比如給args()函數(shù),傳入一個(gè)對(duì)象、數(shù)組和函數(shù):

function fn(){
return 'functions';
}

args('a', 'w3cplus', 'c',{'name': 'airen'},['a',1,3],fn());

// Output:
// => "a"
// => "w3cplus"
// => "c"
// => Object {
// =>     "name": "airen"
// => }
// => Array [
// =>    "a",
// =>    1,
// =>    3
// => ]
// => "functions"

Generators

生成器是一個(gè)函數(shù),它可以退出函數(shù),稍后重新進(jìn)入函數(shù)。

function* generator(){ 
    yield 1; 
    yield 2; 
    yield 3; 
};

for (const g of generator()) { 
    console.log(g); 
}

// Output:
// => 1
// => 2
// => 3

function* 定義一個(gè)生成器函數(shù),該函數(shù)返回生成器對(duì)象。更多關(guān)于生成器相關(guān)的信息,可以點(diǎn)擊這里。

關(guān)閉迭代器

JavaScript中提供了四種已知的終止循環(huán)的方法:break、continue、returnthrow。來(lái)看一個(gè)示例:

const iterable = ['mini', 'mani', 'mo'];

for (const value of iterable) {
console.log(value);
break;
}

// Output:
// => mini

在這個(gè)例子中,我們使用break關(guān)鍵詞來(lái)終止一個(gè)循環(huán),并且只打印出一個(gè)mini。

普通對(duì)象不可迭代

for...of循環(huán)只能和迭代一起工作。但普通對(duì)象是不可迭代的。讓我們看看:

const obj = { fname: 'foo', lname: 'bar' };

for (const value of obj) { // TypeError: obj[Symbol.iterator] is not a function
    console.log(value);
}

在這里,我們定義了一個(gè)普通對(duì)象obj,當(dāng)我們嘗試for...ofobj進(jìn)行操作時(shí),會(huì)報(bào)錯(cuò):TypeError: obj[Symbol.iterator] is not a function。

我們可以把一個(gè)類似數(shù)組的對(duì)象轉(zhuǎn)找成一個(gè)數(shù)組。對(duì)象將具有length屬性,它的元素可以被索引。來(lái)看一個(gè)示例:

const obj = { length: 3, 0: 'foo', 1: 'bar', 2: 'baz' };
const array = Array.from(obj);
for (const value of array) { 
    console.log(value);
}
// Output:
// => foo
// => bar
// => baz

Array.from()方法從類似數(shù)組(Array-lik)或迭代對(duì)象中創(chuàng)建了一個(gè)新的數(shù)組實(shí)例。

for...of vs.for...in

for...in在循環(huán)中將遍歷對(duì)象中所有可枚舉屬性。

Array.prototype.newArr = () => {};
Array.prototype.anotherNewArr = () => {};
const array = ['foo', 'bar', 'baz'];
for (const value in array) { 
    console.log(value);
}
// Outcome:
// => 0
// => 1
// => 2
// => newArr
// => anotherNewArr

for...in不僅可以枚舉數(shù)組里聲明的值,它還可以從構(gòu)造函數(shù)的原型中尋找繼承的非枚舉屬性,比如上例中的newArranotherNewArr,并將它們打印出來(lái)。

for...of可以對(duì)數(shù)組和對(duì)象等做更具體的操作,但并不表示包括所有對(duì)象。

注意:任何具有Symbol.iterator屬性的元素都是可迭代的。

Array.prototype.newArr = function() {};
const array = ['foo', 'bar', 'baz'];
for (const value of array) { 
    console.log(value);
}
// Outcome:
// => foo
// => bar
// => baz

for...of不考慮構(gòu)造函數(shù)原型的不可枚舉屬性。它只需要查找可枚舉屬性并將其打印出來(lái)。

感謝各位的閱讀!關(guān)于JS中的for...of循環(huán)是什么就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

網(wǎng)站欄目:JS中的for...of循環(huán)是什么
標(biāo)題URL:http://bm7419.com/article48/igishp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站改版、網(wǎng)站排名、外貿(mào)建站微信小程序、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)