第 3 卷

const boundParams = [...arguments].slice(1)
4 const boundTargetFunc = this;
5 if (typeof boundTargetFunc !== 'function') { 6
throw new TypeError('the bound target function must be a function.
7 } 8
function fBound() { 9 const restParams = [...arguments]
10const allParams = boundParams.concat(restParams)
| 11 | returnboundTargetFunc.apply(thisinstanceoffBound?this:thisA |
|---|---|
| 12 | } |
| 13 | fBound.prototype=Object.create(boundTargetFunc.prototype) |
| 14 | returnfBound; |
| 15 | } |
手写实现 new 操作符的能力
写实现new,只能函数new程 1.
创建新对象obj 2.
新对象obj的原型指可以通过
- 执构造函数函数
4.
返回这个对象
1 function myNew(Constructor, ...args) { 2
var obj = Object.create(Constructor.prototype)
3 Constructor.call(obj, ...args)
4 return obj;
5 }
手写实现 instanceof
手写实现 Promise

手写 Promise.prototype.catch
是基于有点简单了。
1 Promise.prototype.myCatch = function(onRejected) { 2
return this.then(undefined, onRejected)
3 }
手写 Promise.prototype.finally
这个是有可能考的,比如微信小程序就不支持 finally。可以基于 .then 来实现,不管 fulfilled 还是 rejected 都要执行onFinally。
手写 Promise.all
这个主要是考察如何收集每㇐个 Promise 的状态变化,在最后㇐个 Promise 状态变化时,对外发出信号。
- 判断 iterable 是否空
- 判断 iterable 是否全部不是 Promise
- 遍历,如果某项是 Promise,利用 .then 获取结果,如果 fulfilled,将 value 存在 values 中,并用fulfillCount 计数;如果是 rejected,直接 reject
reason。
- 如果某项不是 Promise,直接将值存起来,并计数。
- 等所有异步都 fulfilled,fulfillCount 必将是 iterable 的长度(在 onFulfilled 中判断 fulfillCount),此时可以 resolve values。
手写防抖节流
防抖
原理:
防抖( debounce):不管事件触发频率多高,㇐定在事件触发 n 秒后才执行,如果你在㇐个事件触发的 n 秒内又触发了这个事件,就以新的事件
的时间为准, n 秒后才执行,总之,触发完事件 n 秒内不再触发事件, n 秒后再执行。(频繁触发就执行最后㇐次)
应用场景:
- 窗口大小变化,调整样式
- 搜索框,输入后 1000 毫秒搜索
- 表单验证,输入1000 毫秒后验证
- 频繁点击按钮,使用防抖避免重复提交请求
防抖实现:
- debunce 实则是个包装函数,通过传入操作函数和时间间隔,来返回㇐个新函数
- 新函数中主要是通过定时器来设置函数调用的频率
- flag 只有第㇐次触发的时候会立即执行
本卷图片
本页图片



































































































本页图片






















本页图片














