推荐的实现迭代器的方法< T>在Typescript中,在ES6之前 [英] Recommended way to implement Iterator<T> in Typescript, before ES6

查看:190
本文介绍了推荐的实现迭代器的方法< T>在Typescript中,在ES6之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多类的项目,理想情况下会实现 Iterable< T> 和/或 Iterator< T> 接口。但是我似乎无法找到这些接口的标准TypeScript定义(例如在typescript-collections或类似的包中)。

I have a project that includes many classes that ideally would implement the Iterable<T> and/or Iterator<T> interfaces. However I cannot seem to find a standard TypeScript definition of these interfaces (for example in typescript-collections or some similar package).

我知道这些在ECMAScript 6中有些标准化通过 Symbol.iterator 机制,但我的目标是ECMAScript 5,并且在可预见的将来会保持这样。

I understand these are somewhat standardized in ECMAScript 6 through the Symbol.iterator mechanism, but my target is ECMAScript 5 and will stay so for the foreseeable future.

我可以以某种方式获取这些接口而不自己定义它们(例如,为了将来与其他模块的兼容性)吗?

Can I somehow get these interfaces without defining them myself (for future compatibility with other modules, for example)?

推荐答案

这是一个副本: typescript:make class objects iterable ,但这里是<$ c的答案$ c> ES5 :

This is a duplicate of: typescript: make class objects iterable, but here's an answer to ES5:

您想使用 ES6 功能

You want to use a ES6 feature:


ECMASc的一个补充ript 2015(ES6)不是新语法或内置的新
,而是一种协议。这个协议可以通过任何符合某些约定的
对象来实现。

One addition of ECMAScript 2015 (ES6) is not new syntax or a new built-in, but a protocol. This protocol can be implemented by any object respecting some conventions.

有两个协议:可迭代协议和迭代器
协议。

There are two protocols: The iterable protocol and the iterator protocol.

ES5 环境(编译和/或运行时)中,这不是什么你可以这么做。

说到这句话,你可以足够接近因为

In a ES5 environment (compilation and/or runtime), and that's not something that you can do.
With that being said, you can get close enough because:


当一个对象知道如何访问一个项目时它是一个迭代器
一次收集一个,同时跟踪其在该序列中的当前位置
。在JavaScript中,迭代器是一个对象,
提供了一个next()方法,该方法返回序列中的下一个项目。
此方法返回一个具有两个属性的对象:done和value。

An object is an iterator when it knows how to access items from a collection one at a time, while keeping track of its current position within that sequence. In JavaScript an iterator is an object that provides a next() method which returns the next item in the sequence. This method returns an object with two properties: done and value.

所以你可以返回一个带有<的对象code> next 方法,它是一个迭代器:

So you can just return an object with a next method and it's an iterator:

class Counter /* implements Iterator<number> */ {
    private counter = 0;

    //public next(): IteratorResult<number> {
    public next(): { done: boolean, value: number } {
        return {
            done: false,
            value: this.counter++
        }
    }
}

let c = new Counter();
console.log(c.next().value); // 0
console.log(c.next().value); // 1
console.log(c.next().value); // 2

操场上的代码

注释掉的部分将与目标 ES6 一起使用,但不会低于该值。

但是如果您的运行时环境确实支持这个功能然后编译好的js就可以正常工作了。

The commented out parts will work with target ES6 but not when it's below that.
But if your runtime environment does support this feature then the compiled js will do the job just fine.

这篇关于推荐的实现迭代器的方法&lt; T&gt;在Typescript中,在ES6之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆