
前端
学习前端相关知识
星宇非凡
这个作者很懒,什么都没留下…
展开
-
reduce函数的实现
参考链接: https://www.cnblogs.com/art-poet/p/12522669.html https://www.runoob.com/jsref/jsref-reduce.html 1. 原函数返回结果: let arr = [1, 2, 3, 4]; const fn = (pre, cur, index, arr) => { console.log(pre, cur, index, arr); return pre + cur; }; let result原创 2021-08-18 11:45:48 · 358 阅读 · 0 评论 -
async-await
参考链接:https://juejin.im/post/5e79e841f265da5726612b6e 1. 准备异步函数 // 异步函数 const getData = () => { return new Promise(resolve => { setTimeout(() => { resolve('data...'); }, 1000); }) } 2. 写代码时async-await的例子 async function test原创 2021-08-16 20:52:25 · 166 阅读 · 0 评论 -
Promise.all 与 Promise.race的实现
1. Promise.all 等待所有的Promise处理完以后,才返回数据,返回时间是处理Promise中耗时最长的那个。 let a = {}; // 传入待处理的Promises, 注意传入的值有可能不全是Promise, 有可能是数值常量等 a.all = function (promises) { // 只考虑传入为数组的情况 const len = promises.length; return new Promise((resolve, reject) =>...原创 2021-08-16 16:15:32 · 200 阅读 · 0 评论 -
forEach, for-of注意点
1. forEach遍历数组时会直接跳过空值,for-of空值则不会 let array = ['a', , 'c']; // a c array.forEach(element => { console.log(element) // 跳过空值 }) // a undefined c for (const item of array) { console.log(item) // 没有跳过空值 } 2. forEach遍历数组时,传入的函数建议使用箭头函数 array.forEa原创 2021-08-16 15:21:06 · 735 阅读 · 0 评论