第一点:bind函数返回一个函数,并且新函数可以传递剩余的参数。
第二点:bind第一个参数为改变的this指向,新函数通过apply绑定传递的第一个参数。
第三点:如果新函数通过new来调用的话,无论是返回结果还是this指向都是new fn的结果。
function fn(a, b, c, d) {
console.log(a, b, c, d)
console.log(this)
return 1
}
let fuc = fn.myBind(1, 2, 3)
let result = new fuc(4, 5)
console.log(result)
Function.prototype.myBind = function (first, ...args) {
let fn = this
return function (...res) {
if (new.target) {
return new fn(...args, ...res)
}
return fn.apply(first, [...args, ...res])
}
}
688

被折叠的 条评论
为什么被折叠?



