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. JSON、GSON

    文章目录 什么是JSON 特点 JSON的数据结构 -- Object JSON的数据结构 -- Array JSON的数据结构 -- 基本类型 构建 JSON 数据 解析 JSON 数据 GSON ...

  2. set object is not JSON serializable 解决方式

    python return json的时候报错: set object is not JSON serializable 解决方式,增加一个将set转为list的函数: def set_default ...

  3. ndarray笔记

    Numpy的介绍 1. Ndarray:N-dimensional array, N维数组 2. 一种由相同类型的元素组成的多维数组,元素数量是事先指定好的 例:建立Ndarray多维数组    nd ...

  4. JS强制关闭浏览器页签并且不提示关闭信息

    工作中很多奇葩的需求都会出现,现在就有一个问题,描述如下: 现在的登录跳转权限页面要去掉,集成在第三方系统信息上,当退出登录的时候需要关掉打开的Tab页面,因此考虑使用window.close()关闭 ...

  5. Centos7.5 安装JDK1.8 步骤

    1.下载JDK安装包 先下载好JDK1.8 的安装包,下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downlo ...

  6. C# Entity Framework The ObjectContext instance has been disposed and can no longer be used for operations that require a connection

    The ObjectContext instance has been disposed and can no longer be used for operations that require a ...

  7. ...:ES6中扩展运算符(spread)和剩余运算符(rest)详解

    1.扩展运算符(spread) demo1:传递数据代替多个字符串的形式 let test= function(a,b,c){ console.log(a); console.log(b); cons ...

  8. 前端开发 Vue -0前言

    Vue2.0 新手完全填坑攻略——从环境搭建到发布 Vue2 入门,读这篇就够了 Jinkey原创感谢 showonne.yubang 技术指导Demo 地址:http://demo.jinkey.i ...

  9. wstngfw中配置snort

    wstngfw中配置snort 概述 Snort是入侵检测和预防系统.它可以将检测到的网络事件记录到日志并阻止它们.Snort使用称为规则的检测签名进行操作. Snort规则可以由用户自定义创建,或者 ...

  10. 二级菜单被banner遮住的解决方法

    原因:z-index的问题 解决:在导航box加  position: relative; z-index: 4; 比banner 大就可以.然后在一级导航的 li 设置 相对定位   positio ...