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. 【洛谷P1343】地震逃生

    一道傻吊的网络流题,wori我写的读入优化怎么老T? 远离读入优化报平安? #include<bits/stdc++.h> #define N 4005 #define inf 10000 ...

  2. GIt 和 Github

    原创 by zoe.zhang        GitHub中采用的比较多得是markdown的语法,博客园里对markdown的支持感觉不是特别友好,但是为了应景,还是用了markdown来写这一篇文 ...

  3. 《Java编程思想》笔记 第七章 复用类

    1.组合 将其他类的对象引用置于新的类中. 3.继承 extends 从已知的一个类中派生出新的一个类,叫子类.子类实现了父类所有 非私有化 非静态 的属性和方法,并能根据自己的实际需求扩展出新的行为 ...

  4. hdu 2236(二分图最小点覆盖+二分)

    无题II Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. Spring Mvc中@ResponseBody中文乱码解决,以及修改返回的Content-Type

    http://www.codeif.com/topic/784 spring 3 mvc 的 @ResponseBody返回数据用起来很方便,但是中文乱码,而且返回的Content-Type不带编码信 ...

  6. 【cocos2d-js官方文档】十二、对象缓冲池

    cc.pool的使用场景 经常创建和销毁的元素,例如打飞机游戏里面的子弹等. 不适用的场景:不是很经常创建的物体,比如背景,建筑等. 如何使用cc.pool 让你的类支持cc.pool 首先,你需在需 ...

  7. Spring Cloud 常用依赖

    <!-- 将微服务provider侧注册进eureka --> <dependency> <groupId>org.springframework.cloud< ...

  8. 51nod 循环数组最大子段和(动态规划)

    循环数组最大子段和 输入 第1行:整数序列的长度N(2 <= N <= 50000) 第2 - N+1行:N个整数 (-10^9 <= S[i] <= 10^9) 输出   输 ...

  9. 21、Flask实战第21天:常用的Flask钩子函数

    在Flask中钩子函数是使用特定的装饰器装饰的函数.为什么叫钩子函数呢?是因为钩子函数可以在正常执行的代码中,插入一段自己想要执行的代码.那么这种函数就叫做钩子函数. before_first_req ...

  10. 【莫队算法】【权值分块】bzoj2223 [Coci 2009]PATULJCI

    不带修改主席树裸题<=>莫队+权值分块裸题. 复杂度O(m*sqrt(n)). P.S.题目描述坑爹,第二个数是权值的范围. #include<cstdio> #include ...