For example we have a 'forEach' method which can loop though a linked list:

forEach(fn) {
let node = this.head;
let counter = ;
while (node) {
fn(node, counter);
node = node.next;
counter++;
}
}

Test:

test('applies a transform to each node', () => {
const l = new List(); l.insertLast();
l.insertLast();
l.insertLast();
l.insertLast(); l.forEach(node => {
node.data += ;
}); expect(l.getAt().data).toEqual();
expect(l.getAt().data).toEqual();
expect(l.getAt().data).toEqual();
expect(l.getAt().data).toEqual();
});

What if we want to use for...of loop? Then we need to convert LinkedList to support generator partten, the usage of for..of:

test('works with the linked list', () => {
const l = new List(); l.insertLast();
l.insertLast();
l.insertLast();
l.insertLast(); for (let node of l) {
node.data += ;
} expect(l.getAt().data).toEqual();
expect(l.getAt().data).toEqual();
expect(l.getAt().data).toEqual();
expect(l.getAt().data).toEqual();
});

Implementation:

*[Symbol.iterator]() {
let node = this.head;
while (node) {
yield node;
node = node.next;
}
}

[Javascript] Convert a forEach method to generator的更多相关文章

  1. [Javascript] The Array forEach method

    Most JavaScript developers are familiar with the for loop. One of the most common uses of the for lo ...

  2. javascript: Convert special characters to HTML

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. [Javascript] Convert a Callback-Based JavaScript Function to a Promise-Based One

    Sometimes, you might want to convert a JavaScript function that accepts a callback to one that retur ...

  4. JavaScript Patterns 5.9 method() Method

    Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...

  5. JavaScript – Convert Image to Base64 String

    From: https://bytenota.com/javascript-convert-image-to-base64-string/ his post shows you two approac ...

  6. 理解JavaScript里的 [].forEach.call() 写法

    原文:  http://www.webhek.com/javascript-foreach-call document.querySelectorAll() 返回的并不是我们想当然的数组,而是 Nod ...

  7. JavaScript中的forEach

    语法:array.forEach(callbackfn[, thisArg]) 参数说明: array1   必需. 一个数组对象. callbackfn   必需. 一个接受最多三个参数的函数. 对 ...

  8. [Javascript] The Array filter method

    One very common operation in programming is to iterate through an Array's contents, apply a test fun ...

  9. [Javascript] The Array map method

    One very common operation in programming is to iterate through an Array's contents, apply a function ...

随机推荐

  1. 还是a+b

    题目描述: 给定 2 个正整数 a, b,a 和 b 最多可能有 40 位,求出 a + b 的和.输入描述: 两个正整数 a, b,a 和 b 最多可能有 40 位.一行表示一个数.输出描述: a ...

  2. 使用QFileInfo类获取文件信息(文件的所有权和权限检查在默认情况下是被禁用的。要使能这个功能 extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;)

    QFileInfo类为我们提供了系统无关的文件信息,包括文件的名字和在文件系统中位置,文件的访问权限,是否是目录或符合链接,等等.并且,通过这个类,可以修改文件的大小和最后修改.读取时间.同时,QFi ...

  3. 谷歌浏览器添加插件时显示程序包无效:"CRX_HEADER_INVALID" 解决办法

    今天在添加谷歌插件的时候,却发现谷歌浏览器显示 程序包无效:"CRX_HEADER_INVALID",现整理解决方法如下: 下图是下载好的 .crx 结尾的插件. 将插件的后缀名改 ...

  4. dotnetcore下解压zip文件,解决中文文件名乱码问题

    (迄今为止网上那些说的用Encoding.Default解决中文文件名乱码的都不能真正解决问题!) 1.在程序开始处 Encoding.RegisterProvider(CodePagesEncodi ...

  5. Linux查看系统及版本信息

    1.查看操作系统版本cat /proc/version 2.查看系统发行版cat /etc/issue 或cat /etc/redhat-release 3.查看系统内核信息uname -a

  6. 一、eureka服务端自动配置

    所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 @EnableEurekaServer开关 eureka是一个c/s架构的服务治理框架, ...

  7. JavaScript--Function对象(函数)的声明和作用域

    Funtion 封装了可重复使用的代码块对象,函数名是一个引用函数对象的变量 声明提前:在程序开始执行之前,将var 变量和function函数提前声明 但赋值并不会提前 它的三种创建方法: 创建1 ...

  8. css的导入与基础选择器

    css是什么 css也是一门标记语言,主要用作修改控制html的样式 css书写的位置(导入) css是用来控制页面标签的样式,但是可以根据实际情况书写在不同的位置, 放在不同位置有不同的专业叫法,可 ...

  9. leetcode-8.atoi · string *

    题面 原题挺长的,还是英文,就不抄了,

  10. echart 不同颜色(柱状图)

    var option = { tooltip: { trigger: 'axis' }, grid: { left: '3%', right: '4%', bottom: '3%', containL ...