美文网首页
call,apply,bind

call,apply,bind

作者: 琉璃_xin | 来源:发表于2019-10-16 15:54 被阅读0次

call

Function.prototype._call = function(targetObject) {
    if (typeof targetObject === 'undefined' || targetObject === null) {
        targetObject = window
    }
    targetObject = new Object(targetObject)

    const targetFnKey = 'targetFnKey'
    targetObject[targetFnKey] = this

    const result = targetObject[targetFnKey](...[...arguments].slice(1))
    delete targetObject[targetFnKey]
    return result
}

apply

Function.prototype._apply = function(targetObject, argsArray) {
    if (typeof argsArray === 'undefined' || argsArray === null) {
        argsArray = []
    }
    if (typeof targetObject === 'undefined' || targetObject === null) {
        targetObject = window
    }
    targetObject = new Object(targetObject)

    const targetFnKey = 'targetFnKey'
    targetObject[targetFnKey] = this

    const result = targetObject[targetFnKey](...argsArray)
    delete targetObject[targetFnKey]
    return result
}

bind

Function.prototype._bind =
    Function.prototype._bind ||
    function(context) {
        if (typeof this !== 'function') {
            throw new Error('Function.prototype.bind -not function')
        }
        var self = this
        //预设参数
        var args = Array.prototype.slice.call(arguments, 1)
        var F = function() {}
        F.prototype = this.prototype

        var fBind = function() {
            var innerArgs = Array.prototype.slice.call(arguments)
            var finalArgs = args.concat(innerArgs)
            return self.apply(this instanceof F ? this : context, finalArgs)
        }

        fBind.prototype = new F()
        return fBind
    }

相关文章

网友评论

      本文标题:call,apply,bind

      本文链接:https://www.haomeiwen.com/subject/geesmctx.html