new
function defineNew(fn, ...args) {
const obj = {}
obj.__proto__ = fn.prototype
fn.apply(obj, args)
return obj
}
call
Function.prototype.gromyCall = function(target,...args){
//此时this是调用者,Function
let context = Symbol('this') //避免key冲突
target[context] = this //把调用者的作用域地址送给目标的一个属性
return target[context](...args) //执行并返回
}
apply
Function.prototype.gromyApply = function(target,args){
let context = Symbol('this');
target[context] = this
return target[context](...args)
}
bind
Function.prototype.gromyBind = function(target,...args){
let context = Symbol('this')
target[context] = this
let result = function(){
return target[context](...args)
}
if(this.prototype){ //保证
result.prototype = this.prototype
}
return result
}