[Javascript] Write a Generator Function to Generate the Alphabet / Numbers
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的更多相关文章
- [ES6系列-07]Generator Function: 生成器函数
[原创]码路工人 Coder-Power 大家好,这里是码路工人有力量,我是码路工人,你们是力量. github-pages 博客园cnblogs Generator function 生成器函数是E ...
- JavaScript自运行函数(function(){})()的理解
今天打开JQuery源文件(jquery-1.8.3), 看到JQuery的初始化过程是这样的 (function( window, undefined ) { // .... })( window ...
- javascript对象模型和function对象
javascript中,函数就是对象 <html> <head> <script type="text/javascript"> functio ...
- JavaScript高级篇之Function对象
JavaScript高级篇之Function对象 一: Function对象引入: Function对象是js的方法对象,可以用Function实例化出任何js方法对象. 例如: <%@ pag ...
- jsp中的javascript的$(document).ready( function() { $("#loginForm").validate()
转自:https://bbs.csdn.net/topics/392459787?list=71147533 下面是jsp页面中的JavaScript代码 $(document).ready( fun ...
- Generator function vs Async/Await
Generator function vs Async/Await generator async /await refs xgqfrms 2012-2020 www.cnblogs.com 发布文章 ...
- React & redux-saga & effects & Generator function & React Hooks
React & redux-saga & effects & Generator function & React Hooks demos https://github ...
- Javascript学习之函数(function)
在JS中,Function(函数)类型实际上是对象;每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也是一个指向函数对象的指针. 一 函 ...
- JavaScript中的Generator函数
1. 简介 Generator函数时ES6提供的一种异步编程解决方案.Generator语法行为和普通函数完全不同,我们可以把Generator理解为一个包含了多个内部状态的状态机. 执行Genera ...
随机推荐
- python--opencv模块
1.图片的读取与展示 #!/usr/bin/env python # -*- coding:utf-8 -*- # author:love_cat import cv2 # 接收两个参数,一个是文件名 ...
- redis使用中的常见错误
1.2016年12月17日 启动redis报错,错误信息如下: 解决办法:redis没有正常关闭(redis安装在虚拟机上,直接杀死了虚拟机进程) 导致redis.pid文件一直被锁定,重启redi ...
- PHP文件环境搭建—EcShop环境搭建
1 rpm -qa|grep yum|xargs rpm -e --nodeps 2 ls 3 rpm -ivh python-iniparse-0.3.1-2.1.el6.noar ...
- ros中删除某个包之后用apt安装的包找不到
原因是工作空间devel里还存有原来的二进制可执行文件,将build和devel内容全删除后再catkin_make就好了
- AC日记——魔方 洛谷 P2007
魔方 思路: 模拟: 代码: #include <cstdio> #include <cstring> #include <iostream> #include & ...
- Spring的自动装配
在Spring中对自定义的引用类型注入时可以实现自动赋值.但是必须依赖set方法: 自动装配功能有两种: <!-- autowire:"byType" --根据class匹 ...
- Python与数据结构[4] -> 散列表[1] -> 分离链接法的 Python 实现
分离链接法 / Separate Chain Hashing 前面完成了一个基本散列表的实现,但是还存在一个问题,当散列表插入元素冲突时,散列表将返回异常,这一问题的解决方式之一为使用链表进行元素的存 ...
- 洛谷P1095 绝地武士的逃离
好吧原题是守望者的逃离,我强行改了一波题面,因为信仰=-=(? May the force be with us. 绝地跑步速度为17m/s,但无法逃离荒岛.绝地的原力恢复速度为4点/s,只有处在原地 ...
- POJ1751 Highways(Prim)
Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13182 Accepted: 3814 Speci ...
- centos7下自定义服务启动和自动执行脚本
systemctl list-units --type=service #查看所有已启动的服务 systemctl enable httpd.service #加入开机自启动服务 systemctl ...