JavaScript AOP 编程模块实现与用法
作者:纸飞机时间:2026-10-29 12:31:39浏览:181
本文介绍一个简洁的 JavaScript AOP 模块,支持 before、after 和 around 三种切面类型。通过配置 context 和 target 指定目标函数,并注入前置、后置及环绕逻辑,实现函数执行前后的拦截与增强。
JavaScript AOP 模块概述
本文通过一个具体示例,展示如何在 JavaScript 中实现面向切面编程(AOP)。该示例旨在为读者提供可参考的代码实现与使用思路。
AOP 模块配置与使用示例
/*
// aop({options});
// By: adamchow2326@yahoo.com.au
// Version: 1.0
// Simple aspect oriented programming module
// support Aspect before, after and around
// usage:
aop({
context: myObject, // scope context of the target function.
target: "test", // target function name
before: function() { // before function will be run before the target function
console.log("aop before");
},
after: function() { // after function will be run after the target function
console.log("aop after");
},
around: function() { // around function will be run before and after the target function
console.log("aop around");
}
});
*/
var aop = (function() {
var options = {},
context = window,
oFn,
oFnArg,
targetFn,
targetFnSelector,
beforeFn,
afterFn,
aroundFn,
cloneFn = function(Fn) {
if (typeof Fn === "function") {
return eval('[' +Fn.toString()+ ']')[0];
}
return null;
},
checkContext = function() {
if (options.context) {
context = options.context;
}
if (typeof context[(options.target).name] === "function") {
targetFnSelector = (options.target).name;
targetFn = context[targetFnSelector];
}
else if (typeof context[options.target] === "function") {
targetFnSelector = options.target;
targetFn = context[targetFnSelector];
}
if (targetFn) {
oFn = cloneFn(targetFn);
oFnArg = new Array(targetFn.length);
return true;
}
else {
return false;
}
},
run = function() {
context[targetFnSelector] = function(oFnArg) {
if (aroundFn){
aroundFn.apply(this, arguments);
}
if (beforeFn){
beforeFn.apply(this, arguments); // 'this' is context
}
oFn.apply(this, arguments);
if (afterFn){
afterFn.apply(this, arguments); // 'this' is context
}
if (aroundFn){
aroundFn.apply(this, arguments);
}
};
};
return function(opt){
if (opt && typeof opt === "object" && !opt.length) {
options = opt;
if (options.target && checkContext()) {
if (options.before && typeof options.before === "function") {
beforeFn = options.before;
}
if (options.after && typeof options.after === "function") {
afterFn = options.after;
}
if (options.around && typeof options.after === "function") {
aroundFn = options.around;
}
run();
}
}
};
})();
// test examples
// ----------------- aop modify global function ---------------//
function test(name, age) {
console.log("test fn. name = " + name + " age: " + age);
}
aop({
target: "test",
before: function() {
console.log("aop before");
},
after: function() {
console.log("aop after");
},
around: function() {
console.log("aop around");
}
});
// run
test("adam", 6);
// ----------------- aop test modify method in an object ---------------//
var myobj = {
myName: "testName",
sayName: function() {
console.log(this.myName);
},
childObj: {
age: 6,
say: function() {
console.log(this.age);
}
}
};
aop({
context: myobj,
target: "sayName",
before: function() {
console.log("aop before say name = " + this.myName);
},
after: function() {
console.log("aop after say name = " + this.myName);
},
around: function() {
console.log("aop around say name = " + this.myName);
}
});
// run
myobj.sayName();
aop({
context: myobj.childObj,
target: "say",
before: function() {
console.log("aop before say name = " + this.age);
},
after: function() {
console.log("aop after say name = " + this.age);
},
around: function() {
console.log("aop around say name = " + this.age);
}
});
myobj.childObj.say();
希望本文提供的代码示例与说明,能对读者在 JavaScript 程序设计中的应用有所帮助。
作者最新文章
JS动态构建复杂嵌套JSON对象的方法
2026-11-05 15:41:52
JS负数取余为什么是负数?详解余数与模运算区别
2026-11-05 13:34:41
JS 解压出现乱码或报错怎么解决?
2026-11-05 12:32:38
JS 中 startsWith、indexOf 与正则判断字符串前缀的对比
2026-11-04 22:19:43
JS解析Excel后怎么将数据转换为JSON格式
2026-11-04 20:58:28
