七月上 2022-05-04 13:56:38
class LazyMan { constructor (user) { this.timeList = []; console.log(`Hi I am ${user}`) Promise.resolve().then(res => this.next()); } eat (res) { var fn = () => { console.log(`I am eating ${res}`); this.next(); } this.timeList.push(fn); return this; } sleep (time) { var fn = res => { setTimeout(res => { console.log(`等待了${time}秒`); this.next(); },time*1000) } this.timeList.push(fn); return this; } next () { var fn = this.timeList.shift(); fn && fn(); } sleepFrist (time) { var fn = res => { setTimeout(res => { console.log(`先等待了${time}秒`); this.next(); },time*1000) } this.timeList.unshift(fn); return this; } } function lazyman (res) { return new LazyMan(res) } _//lazyman('静静').sleep(2).eat('饺子').sleep(3).eat('面').sleepFrist(1).eat('others') //Hi I am 静静 //先等待了1秒 //等待了2秒 //I am eating 饺子 //等待了3秒 //I am eating 面 //I am eating others_
七月上 2022-05-04 13:56:37
原因很简单,一个父组件下不只有你一个子组件。
同样,使用这份 prop 数据的也不只有你一个子组件。
如果每个子组件都能修改 prop 的话,将会导致修改数据的源头不止一处。
所以我们需要将修改数据的源头统一为父组件,子组件像要改 prop 只能委托父组件帮它。从而保证数据修改源唯一
- 共 1 页
- 1
我优化了一下节流的逻辑,保证最后一次必须执行。
第 3 题:什么是防抖和节流?有什么区别?如何实现?