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

Javascript 可迭代的技術定義是什么,您如何對其進

What is the technical definition of a Javascript iterable and how do you test for it?(Javascript 可迭代的技術定義是什么,您如何對其進行測試?)
本文介紹了Javascript 可迭代的技術定義是什么,您如何對其進行測試?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我一直在實現 ES6 Set 對象的一個??有用的子類.對于我的許多新方法,我希望接受一個參數,該參數可以是另一個 Set 或 Array,或者實際上是我可以迭代的任何東西.我一直在我的界面中將其稱為可迭代",并在其上使用 .forEach() (這對于 Set 或 Array 來說很好.示例代碼:

I've been implementing a useful subclass of the ES6 Set object. For many of my new methods, I want to accept an argument that can be either another Set or an Array, or really anything that I can iterate. I've been calling that an "iterable" in my interface and just use .forEach() on it (which works fine for a Set or an Array. Example code:

// remove items in this set that are in the otherIterable
// returns a count of number of items removed
remove(otherIterable) {
    let cnt = 0;
    otherIterable.forEach(item => {
        if (this.delete(item)) {
            ++cnt;
        }
    });
    return cnt;
}

或者

// add all items from some other iterable to this set
addTo(iterable) {
    iterable.forEach(item => {
        this.add(item);
    });
}

但是,我懷疑我可能并不真正支持 ES6 定義的任何可迭代對象,所以我對 Javascript 可迭代對象的真正定義使用 ES6 規范中的術語感興趣?

But, I suspect I may be not really supporting any iterable in the way that ES6 defines it so I'm interested in what the real definition of a Javascript iterable is using the term as the ES6 specification does?

如何在 ES6 Javascript 中對其進行測試?

How do you test for it in ES6 Javascript?

你應該如何迭代一個通用的可迭代對象?

How should you iterate a generic iterable?

我在 ES6 規范中發現了這樣的短語:

I've found phrases like this in the ES6 specification:

如果參數iterable存在,它應該是一個對象實現了一個返回迭代器對象的@@iterator 方法生成一個二元素類數組對象,其第一個元素是將用作 WeakMap 鍵的值,其第二個元素是與該鍵關聯的值.

If the parameter iterable is present, it is expected to be an object that implements an @@iterator method that returns an iterator object that produces a two element array-like object whose first element is a value that will be used as a WeakMap key and whose second element is the value to associate with that key.

但是,這指的是一個 @@iterator 方法,我似乎無法通過該屬性名稱訪問它.

But, that refers to an @@iterator method which I don't seem to be able to access via that property name.

推薦答案

像 ES6 規范那樣使用術語的 Javascript 可迭代的真正定義是什么?

What is the real definition of a Javascript iterable using the term as the ES6 specification does?

§25.1.1.1 定義"Iterable 接口".

它們是帶有 Symbol.iterator-keyed 方法的對象,該方法返回一個有效的 Iterator (這反過來又是一個預期按照 §25.1.1.2).

They're objects with a Symbol.iterator-keyed method that returns a valid Iterator (which in turn is an object expected to behave as it should according to §25.1.1.2).

如何在 ES6 Javascript 中對其進行測試?

How do you test for it in ES6 Javascript?

不調用@@iterator方法就無法測試返回的內容,不嘗試運行就無法測試結果是否符合Iterator接口.最好的辦法是這樣做

We cannot test what the @@iterator method returns without calling it, and we cannot test whether the result conforms to the Iterator interface without trying to run it. The best bet would be to do

function looksIterable(o) {
    return typeof o[Symbol.iterator] == "function";
}

但是我通常不會對此進行測試,而只是在它不可迭代時讓它失敗并拋出異常.

however I wouldn't usually test for this but simply let it fail with an exception when it's not iterable.

你應該如何迭代一個通用的可迭代對象?

How should you iterate a generic iterable?

不要使用 forEach.(事實上,不要在 ES6 的任何地方使用 forEach).

正確的迭代方式是 for (... of ...) 循環.它會檢查所有的可迭代性(使用抽象的 GetIterator 操作 并運行(甚至關閉)迭代器,并在用于不可迭代的值時拋出適當的 TypeError.

The proper way to iterate is a for (… of …) loop. It does all the checking for iterability (using the abstract GetIterator operation and running (and even closing) the iterator, and throws appropriate TypeErrors when used on non-iterable values.

這篇關于Javascript 可迭代的技術定義是什么,您如何對其進行測試?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 毛片视频网站 | 亚洲天堂网站 | 久久都是精品 | 91av在线免费观看 | av黄色大片 | 欧美日韩三级 | 国产福利久久 | 在线视频91 | 在线欧美| 日韩欧美国产综合 | 国产日韩久久 | 欧美三级又粗又硬 | 亚洲一区二区欧美 | 清清草视频| 免费看一级黄色片 | 国产日韩欧美 | 日韩精品网 | 久色成人| 玖玖精品在线 | 欧洲色综合 | 视频一区二区在线 | 日韩精品免费在线观看 | 91福利在线观看 | 黄色片免费观看 | www.av在线播放| 91久久精品日日躁夜夜躁欧美 | 在线观看的av网站 | 日韩av在线免费播放 | 黄色片免费网站 | 69成人网| 久久精品在线视频 | 日韩毛片网 | 亚洲丝袜视频 | 国产精品第二页 | a毛片在线观看 | 久草网在线观看 | 青草福利视频 | 一区 | 欧美激情久久久 | 午夜精品视频 | 中文字幕一区在线观看 |