Function.prototype.before = function (beforeFn) { let _this = this return function () { beforeFn.apply(this, arguments) return _this.apply(this, arguments) } } Function.prototype.after = function (afterFn) { let _this = this return function () { let res = _this.apply(this, arguments) afterFn.apply(this, arguments) return res } } function test() { console.log('11111111111'); } let fn = test.before(() => { console.log('before'); }).after(() => { console.log('after'); })
function log() { console.log('logging'); } function render() { console.log('rendering'); } //如果需要记录,就扩展 render = render.before(log) let btn = document.querySelector('button') btn.addEventListener('click', () => { render() })