When you need to generate a data set, a generator function is often the correct solution. A generator function is defined with function* and then you yield each result you want out of the function when you call it. Generators pair well with the ... operator inside of Array because they serve as iterators which can be called until all their results are exhausted.

function* generateAlphabet() {
let i = "a".charCodeAt();
let end = "z".charCodeAt() + ; while (i < end) {
yield String.fromCharCode(i);
i++;
}
} let alphabet = [...generateAlphabet()]; Array.prototype.fill = function* generateNumber(val) {
let i = ;
while (i < Number(this.length)) {
yield typeof val === "function" ? val(i) : val;
i++;
}
};
var numbers = [...new Array().fill()];
console.log(numbers); // [undefined, undefined, undefined, undefined, undefined, undefined]
var numbers = [...new Array().fill()];
console.log(numbers); // [0, 0, 0, 0, 0, 0]
var numbers = [...new Array().fill(i => i * )];
console.log(numbers); //[0, 2, 4, 6, 8, 10]

[Javascript] Write a Generator Function to Generate the Alphabet / Numbers的更多相关文章

  1. [ES6系列-07]Generator Function: 生成器函数

    [原创]码路工人 Coder-Power 大家好,这里是码路工人有力量,我是码路工人,你们是力量. github-pages 博客园cnblogs Generator function 生成器函数是E ...

  2. JavaScript自运行函数(function(){})()的理解

    今天打开JQuery源文件(jquery-1.8.3), 看到JQuery的初始化过程是这样的 (function( window, undefined ) { // .... })( window ...

  3. javascript对象模型和function对象

    javascript中,函数就是对象 <html> <head> <script type="text/javascript"> functio ...

  4. JavaScript高级篇之Function对象

    JavaScript高级篇之Function对象 一: Function对象引入: Function对象是js的方法对象,可以用Function实例化出任何js方法对象. 例如: <%@ pag ...

  5. jsp中的javascript的$(document).ready( function() { $("#loginForm").validate()

    转自:https://bbs.csdn.net/topics/392459787?list=71147533 下面是jsp页面中的JavaScript代码 $(document).ready( fun ...

  6. Generator function vs Async/Await

    Generator function vs Async/Await generator async /await refs xgqfrms 2012-2020 www.cnblogs.com 发布文章 ...

  7. React & redux-saga & effects & Generator function & React Hooks

    React & redux-saga & effects & Generator function & React Hooks demos https://github ...

  8. Javascript学习之函数(function)

    在JS中,Function(函数)类型实际上是对象;每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也是一个指向函数对象的指针. 一 函 ...

  9. JavaScript中的Generator函数

    1. 简介 Generator函数时ES6提供的一种异步编程解决方案.Generator语法行为和普通函数完全不同,我们可以把Generator理解为一个包含了多个内部状态的状态机. 执行Genera ...

随机推荐

  1. CSS入门之定义和应用格式

    1:定义 1:定义单个属性 property:value 例如: color:white(属性和值之间用冒号隔开) 2:定义多个属性 property:value;property:value 例如: ...

  2. Nginx-Primary script unknown的报错的解决方法

    配置nginx时一直报错:file not found 错误日志: [error] 12691#0: *6 FastCGI sent in stderr: "Primary script u ...

  3. [BZOJ2653]middle 主席树+二分

    2653: middle Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2042  Solved: 1123[Submit][Status][Disc ...

  4. (九)expect批量公钥推送

    (1)expect实现ssh非交互登录 注意:注释不能出现这脚本里面 spawn表示开启一个会话 \r:表示回车,exp_continue :表示没有出现这样,继续往下执行 interact :停留在 ...

  5. 自定义的一个JDBC工具类

    package JDBCutils; import java.io.File;import java.io.FileInputStream;import java.sql.Connection;imp ...

  6. SnowNLP:一个处理中文文本的 Python 类库[转]

    SnowNLP是一个python写的类库,可以方便的处理中文文本内容,是受到了TextBlob的启发而写的,由于现在大部分的自然语言处理库基本都是针对英文的,于是写了一个方便处理中文的类库,并且和Te ...

  7. HDU1009:FatMouse' Trade(初探贪心,wait)

    FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containi ...

  8. uestc1633

    uestc1633 题意 给你一个大小为 \(n\) 的集合 \(S\) ,集合里有 \(n\) 个互不相同正整数,有 \(q\) 个询问,每次询问是否能选择 \(S\) 中的一些数字 ( 同一个数字 ...

  9. Typora

    Typora BB in front 如果你是一个佛(lan)系(duo),内心文艺的程序员,并且你对其他Markdown编辑器的使用效果感觉不是很好的话,可以来了解一下该软件Typora. What ...

  10. Problem T: 零起点学算法15——交换变量

    #include<stdio.h> int main() { int a,b,c; scanf("%d %d",&a,&b); c=a; a=b; b= ...