[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 ...
随机推荐
- Mybatis三剑客
1.Mybatis-generator 自动化生成数据库交互代码->dao+pojo+xml 2.Mybatis-plugin dao文件和xml自动跳转,验证正确性,在xml中只能提示等功能 ...
- Javacore分析(转载)
本文转自(http://www.ibm.com/developerworks/cn/websphere/library/techarticles/1406_tuzy_javacore/1406_tuz ...
- servlet(2) - 利用MyEclipse新建一个servlet - 小易Java笔记
1.Tomcat在MyEclipse中集成 ==> Window-preferences-MyEclipse-Servers-Tomcat-Tomcat 6.x-点击右侧的Browse,选择你的 ...
- 键盘焦点和逻辑焦点(Logic Focus与Keyboard Focus )
键盘焦点和逻辑焦点(Logic Focus与Keyboard Focus ) 1.定义Keyboard Focus可以理解为物理焦点.就是整个桌面上可以响应键盘输入的地方,整个桌面在某个时刻只可能有一 ...
- DRF视图集的路由设置
在使用DRF视图集时,往往需要配一大堆路由,例如: # views.py class DepartmentViewSet(ListModelMixin,CreateModelMixin,Retriev ...
- 《Java编程思想》笔记 第十二章 通过异常处理错误
1.异常也是对象 标准异常类都有两个构造器,一个默认,一个接受字符串. 抛异常与方法返回类型不同,但有相似效果使当前方法退出并返回,抛异常可以看作是一种不同的返回机制.(异同点不必深究) Throwa ...
- k8s的chart学习(上)
chart 是 Helm 的应用打包格式.chart 由一系列文件组成,这些文件描述了 Kubernetes 部署应用时所需要的资源,比如 Service.Deployment.PersistentV ...
- Qt笔记——Event
#ifndef MYBUTTON_H #define MYBUTTON_H #include <QPushButton> class MyButton : public QPushButt ...
- box-shadow用法简介
语法: <strong>box-shadow:</strong><em><length></em><em><length& ...
- django web 自定义通用权限控制
需求:web系统有包含以下5个url,分别对于不同资源: 1.stu/add_stu/ 2.stu/upload_homework/ 3.stu/query_homework/ 4.stu/add_r ...