ES6 - symbol&generator
栏目: JavaScript · 发布时间: 7年前
-
ES6 新增基本数据类型(不常用),现在总共7种基本数据类型
let syml = Symbol('aaa'); typeof(syml) //symbol 复制代码 -
注意:
- 不是constructor,不能new
- Symbol() 返回一个唯一值,定义唯一或私有的东西;可以作为key,不能被for in 访问;
- symbol是一种基本数据类型;
generator函数 (生成器)
- 目的:配合promise使用,解决异步,深度嵌套,(见promise),现在用的变少,被async取代;
-
语法:星号 * 和 yield:
//定义 function * gen(){ yield 'welcome'; yield 'to'; return 'China'; } //手动调用 next()顺序执行 let g1 = gen(); console.log(g1.next()); // {value:'welcome', done: false} console.log(g1.next()); // {value:'to', done: false} console.log(g1.next()); // {value:'China', done: ture} //用 for of 循环 for(let val of g1){ console.log(val); //welcome to; return 的东西不会遍历 } 复制代码 -
generator不仅可以用for of, 还可以解构赋值,或使用扩展运算符;
let [a, b] = gen(); //a, b = welcome to -
generator配合axios数据请求,实例:
function * gen(){ let val = yield 'murphy'; yield axios.get('https://api..../${val}') //val为传参 } let gi = gen(); let username = g1.next().value; g1.next(username).value.then(res=>{ console.log(res.data); }); 复制代码
异步解决方案总结:
- 回调函数
- 监听事件
- 发布订阅
- promise对象
- async
以上所述就是小编给大家介绍的《ES6 - symbol&generator》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Pattern Recognition and Machine Learning
Christopher Bishop / Springer / 2007-10-1 / USD 94.95
The dramatic growth in practical applications for machine learning over the last ten years has been accompanied by many important developments in the underlying algorithms and techniques. For example,......一起来看看 《Pattern Recognition and Machine Learning》 这本书的介绍吧!
URL 编码/解码
URL 编码/解码
RGB CMYK 转换工具
RGB CMYK 互转工具