热门

最新

红包

立Flag

投票

同城

我的

发布
cc18868876837
码飞_CC
5 年前
truecc18868876837

bind方法实现:

Function.prototype.bind = function(context, ...args) {
if (typeof this !== 'function') {
throw new Error("Type Error");
}
// 保存this的值
var self = this;

return function F() {
// 考虑new的情况
if(this instanceof F) {
return new self(...args, ...arguments)
}
return self.apply(context, [...args, ...arguments])
}
}

CSDN App 扫码分享
分享
评论
2
打赏
  • 复制链接
  • 举报
下一条:
call方法的实现:Function.prototype.call = function(context = window, ...args) { if (typeof this !== 'function') { throw new TypeError('Type Error'); } const fn = Symbol('fn'); context[fn] = this; const res = context[fn](...args); delete context[fn]; return res;}
立即登录