bind.apply.call

50 阅读1分钟
Function.prototype.newApply=function(context){
    context=context||window
    context.fn=this
    const result=arguments[1]?context.fn(...arguments[1]):context.fn()
    delete context.fn
    return result
}
Function.prototype.newBind=function(){
    const _this=this
    const newObj=Array.prototype.slice.call(arguments)
    const newThis= newObj.shift()
    return function(){
        return _this.newApply(newThis,newObj)
    }
}

Function.prototype.newCall=function(context){
    const newObj=Array.prototype.slice.call(arguments)
    newObj.shift()
    context=context||window
    context.fn=this
    const result =newObj.length>0? context.fn(...newObj):context.fn()
    delete context.fn
    return result

}

var a={name:'name'}
var b={
    name:'b',
    getName:function(first,second){
        return this.name+':first:'+first+".second:"+second
    }
}
//b.getName.newBind(a)
let aa=b.getName.newCall(a,'one','two')
let a1=b.getName.newApply(a,['one','two'])
let a2=b.getName.newBind(a,'one','two')()