Skip to content
📁 标签
JavaScript手写题

softBind函数

softBindbind函数一样,返回一个指向this的新的函数,但bind函数多次调用只会将this指向第一次绑定的值,而softBind指向最后绑定的值

js
Function.prototype.softBind = function (thisAry, ...args) {
    let fn = this
    // 接收剩下的参数
    const bound = function (...content) {
        const o = !this || this === (window || global) ? thisAry : this
        return fn.apply(o, [...args, ...content])
    }
    // 替换原型
    bound.prototype = Object.create(fn.prototype)
    return bound
}