手写bind,我这样可以吗?
来源:6-5 作用域相关的面试题 - part1

香饽饽0
2021-04-12
Function.prototype.myBind = function(ctx, ...rest) {
return (...innerRest) => {
return this.apply(ctx, [...rest, ...innerRest])
}
}
function fn(...rest) {
console.log(this, rest);
}
fn.myBind({a : 1}, 1 ,2)(3, 4)
// 输出: {a: 1} (4) [1, 2, 3, 4]
写回答
1回答
-
功能是没问题。
不过,你用了 apply 和 ES6 的解构语法。你想,一般情况下,只要支持 apply 和 ES6 的解构语法 ,那它也肯定支持 bind ,对吧?
所以,你的这种方式,写出来没有真实的应用场景。
012021-04-12
相似问题