随笔分类 - ES6
摘要:背景,需要发起一个网络请求(setTimeout模拟),3秒得到结果。 要求必须在这个网路请求之后才能访问请求到的数据myRes。 要求调用方f4() 和 发起网络请求的f3()都必须用async/await。 假如f4()不用async/await,f3()用。代码执行到promise内部,执行完
阅读全文
摘要:1. // Example POST method implementation: async function postData(url='15479453.html', data={}) { // Default options are marked with * const response
阅读全文
摘要:参考书籍链接:https://es6.ruanyifeng.com/#docs/generator-async 1.Symbol.hasInstance 对象的 Symbol.hasInstance 属性,指向一个内部方法。当其他对象使用 instanceof 运算符,判断是否为该对象的实例时,会调
阅读全文
摘要:结论先行: yield 句本身没有返回值,或者说总是返回 undefined 。 next 方法可以带一个参数,该参数就会被当作上一个 yield 语句的返回 值。 1. next()不带参数 function* gen(x) { for (var x of [1,2,3,4,5]){ var y
阅读全文
摘要:1. 生产环境的转换器,建议使用 Thunkify 模块。 // "use strict" function thunkifyy(fn) { return function() { console.log(arguments); //Arguments(2) [8, 9, callee: ƒ, Sy
阅读全文
摘要:1. JavaScript 语言的 Thunk 函数 JavaScript 语言是传值调用,它的 Thunk 函数含义有所不同。在 JavaScript 语言中,Thunk 函数替换的不是表达式,而是 多参数函数,将其替换成一个只接受回调函数作为参数的单参数函数。 // ES6版本 var Thun
阅读全文
摘要:1.带参数(10),需使用return。并且下一次调用next没有用了。 function* gen(x) { for (var x of [1,2,3,4,5]){ var y = yield (x + 2); return y+10; } } var g = gen(1); console.lo
阅读全文
摘要:function* gen(x){ try { var y = yield x + 2; } catch (e){ console.log(e); console.log("进入异常catch输出"); } return y; } var g = gen(1); console.log(g.next
阅读全文
摘要:构造函数如果采用以下这种方式声明,并不能为Point类生成一个名name为‘y1’的属性property。 Object.assign(Point.prototype, { constructor(x,y){ this.x=x; this.y1=y; }, fun1(){} }); 完整demo如下
阅读全文