简介
装饰器模式能够很好的对已有功能进行拓展,这样不会更改原有的代码,对其他的业务产生影响,方便我们在较小的改动下对软件功能进行拓展。
Function.prototype.before = function (beforeFn) {
var _this = this;
return function () {
beforeFn.apply(this, arguments);
return _this.apply(this, arguments);
};
};
Function.prototype.after = function (afterFn) {
var _this = this;
return function () {
const result = _this.apply(this, arguments);
afterFn.apply(this, arguments);
return result;
};
};
function test() {
console.log(111);
}
var test1 = test
.before(() => {
console.log("before");
})
.after(() => {
console.log("after");
});
test1();