秋雨
首页
  • HTML
  • CSS
  • JavaScript
设计模式
webpack
  • 前端常识
  • ES
  • React
  • Redux
  • ReactRouter
  • 事件循环
  • 浏览器渲染流程
  • Vue项目性能优化
  • Vue
  • App
Github
首页
  • HTML
  • CSS
  • JavaScript
设计模式
webpack
  • 前端常识
  • ES
  • React
  • Redux
  • ReactRouter
  • 事件循环
  • 浏览器渲染流程
  • Vue项目性能优化
  • Vue
  • App
Github
  • 构造器模式
  • 原型链模式
  • 工厂模式
  • 抽象工厂模式
  • 建造者模式
  • 单例模式
  • 装饰器模式
  • 适配器模式
  • 适配器模式
  • 代理模式
  • 观察者模式
  • 发布订阅模式
  • 模块模式
  • 桥接模式
  • 组合模式
  • 命令模式
  • 命令模式
  • 迭代器模式
  • 职责链模式

简介

装饰器模式能够很好的对已有功能进行拓展,这样不会更改原有的代码,对其他的业务产生影响,方便我们在较小的改动下对软件功能进行拓展。

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();
最后更新:
贡献者: Mrlishizhen
Prev
单例模式
Next
适配器模式