call
Function.prototype.mCall = function (_this,...args){
if (!_this) _this = Object.create(null)
_this.fn = this
let res = _this.fn(...args)
delete _this.fn
return res
}
apply
Function.prototype.mApplay = function (_this,args=[]){
if (!Array.isArray(args)){
return TypeError('参数必须是数组')
}
if (!_this) _this = Object.create(null)
_this.fn = this
let res = _this.fn(...args)
delete _this.fn
return res
}
new
Function.prototype.mNew = function(fn,...args){
const instance = Object.create(fn.prototype)
const res = fn.apply(instance,args)
return typeof res === "object" ? res : instance;
}
bind
Function.prototype.mBind= function (_this,...args){
const fn = this
return function F(...args2){
return this instanceof F ? fn(...args,...args2) :fn.apply(_this,args.concat(args2))
}
}