1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| function execFn(thisArg,otherArgs,fn){ thisArg = (thisArg === null || thisArg === undefined) ? window : Object(thisArg) Object.defineProperty(thisArg,'fn',{ configurable:true, writable:true, enuramble:false, value:fn }) thisArg.fn(...otherArgs) delete thisArg.fn } Function.prototype.hyApply = function(thisArg,otherArgs){ execFn(thisArg,otherArgs,this) }
Function.prototype.hyCall = function(thisArg,...otherArgs){ execFn(thisArg,otherArgs,this) }
function foo(name,age){ console.log(this,name,age) } foo.hyCall('123','kxs',22) foo.hyApply('123',['kxs',22])
|