AOP在js中的实现,先看看用法吧:

        var A2D = $.noConflict();//不要误会,此乃我自己写的A2D框架,非jQuery

        function fn1(name, age) {
console.log("name: " + name);
console.log("age: " + age);
}
function b1() {
console.log("b1");
console.log("b1, parameters - 0: " + arguments[0]);
console.log("b1, parameters - 1: " + arguments[1]);
console.log("b1, parameters - 2: " + arguments[2]);
console.log("b1, parameters - 3: " + arguments[3]);
console.log("b1, parameters - 4: " + arguments[4]);
console.log("b1, parameters - 5: " + arguments[5]);
}
function b2() {
console.log("b2");
}
function a1() {
console.log("a1");
}
function a2() {
console.log("a2");
} var aopfn1 = A2D.aop(fn1, {
before: [b1, b2],
after: [a1, a2]
});
aopfn1.execute("aaron", 20);

核心A2D代码实现(before和after AOP,实现了6个参数):

function aop(fn, config) {
if (!fn instanceof Function)
alert("fn must be a function");
if (config == null || config == undefined || !config instanceof Object)
alert("config can not be empty"); function aopWrapper(fn, config) {
this.realFunction = fn;
this.beforeFunctions = config.before.concat();
this.afterFunctions = config.after.concat();
}
aopWrapper.prototype.execute = function () {
if (this.beforeFunctions)
for (var fn in this.beforeFunctions)
this.beforeFunctions[fn].call(this.realFunction, arguments[0],
arguments[1],
arguments[2],
arguments[3],
arguments[4],
arguments[5]);
this.realFunction.call(this.realFunction, arguments[0],
arguments[1],
arguments[2],
arguments[3],
arguments[4],
arguments[5]);
if (this.afterFunctions)
for (var fn in this.afterFunctions)
this.afterFunctions[fn].call(this.realFunction, arguments[0],
arguments[1],
arguments[2],
arguments[3],
arguments[4],
arguments[5]);
} wrapper = new aopWrapper(fn, config); return wrapper;
}

有图有真相:

搞前端的,为什么这么难。。。。

最新文章

  1. 在Linux上配置Zabbix的环境
  2. IOS App 右上脚红色数字提醒
  3. 5.3(2)----机器人走方格2(CC150)
  4. 快速排序C++
  5. redhat enterprixe 5.0 DNS 服务配置与管理
  6. JAVA中继承时方法的重载(overload)与重写/覆写(override)
  7. 使用HttpClient发送请求、接收响应
  8. 使用libCurl实现断点下载
  9. 【转】SQL Server 2008 新类型介绍之Date和Time
  10. webapp之路--之必备知识
  11. Docker 移除镜像
  12. [区块链] 密码学——椭圆曲线密码算法(ECC)
  13. FliterLog代码分析
  14. 隐藏用户建立(Powershell)
  15. Ali流量控制中间件Sentinel
  16. leveldb 学习记录(四)Log文件
  17. sixsix团队M2阶段Postmortem
  18. RNN,LSTM,GRU简单图解:
  19. linux动态库
  20. Sublime Text 中文

热门文章

  1. 不需要再手写 onSaveInstanceState 了,因为你的时间非常值钱
  2. DAY5(PYTHON) 字典的增删改查和dict嵌套
  3. (网页)HTML5
  4. Orchard详解--第三篇 依赖注入之基础设施
  5. Orchard详解--第二篇 启动
  6. 【redis专题(7)】命令语法介绍之Pub/Sub
  7. mysql初始化提示安装perl
  8. Linux平台下RMAN异机恢复总结
  9. C#面向对象 类的继承
  10. MySQL 8.0.11安装配置