Function.prototype.muCall = function(context,...args){
context = Object(context) || window
let key = Symbol('own')
context[key] = this
args = args || []
let res = context[key](...args)
delete context[key]
return res
}
Function.prototype.muapply = function(context,args) {
context = Object(context) || window
args = args ? args : []
const key = Symbol()
context[key] = this
const result = context[key](...args)
delete context[key]
return result
}
Function.prototype.mubind = function(context,...args) {
const fn = this
args = args || []
return function newFn(...newFnArgs) {
if(this instanceof newFn) {
return new fn(...args,...newFnArgs)
}
return fn.apply(context,[...args,...newFnArgs])
}
}
function sayHelloTo (to,fo) {
console.log(`${this.name} say hello to ${to}-${fo}`)
}
var Foo = {
name: 'Foo'
}
sayHelloTo.muCall(Foo,'a','b')