// hook function Function.prototype.hook = function (hookFunc, context) { if (this.prototype && this.prototype.isHooked) { console.warn(`Warning! Function ${this.name} has been hooked.`); returnfalse; }
context = context || window; let _name = this.name; try { let _cb = context[_name]; context[_name] = function () { var args = Array.prototype.slice.call(arguments, 0); hookFunc.apply(this, args); return _cb.apply(this, args); }; returntrue; } catch (e) { console.error(`Failed! Function ${this.name} hook failed,check the params.`, e); returnfalse; } }
// unhook function Function.prototype.unhook = function (context) { if (!this.prototype.isHooked) { console.warn(`Warning! No function is hooked on.`); returnfalse; }
Author
My name is Micheal Wayne and this is my blog.
I am a front-end software engineer.
Contact: michealwayne@163.com