JavaScript之AOP編程實例
更新時間:2015年07月17日 14:33:25 作者:紅薯
這篇文章主要介紹了JavaScript的AOP編程,以實例形式分析了javascript面向切面編程的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了JavaScript之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程序設(shè)計有所幫助。
相關(guān)文章
uniapp的webview實現(xiàn)左滑返回上一個頁面操作方法
uniapp默認(rèn)左滑是關(guān)閉整個webview,而不是關(guān)閉當(dāng)前頁,本文給大家介紹uniapp的webview實現(xiàn)左滑返回上一個頁面操作方法,感興趣的朋友一起看看吧2023-12-12
JavaScript函數(shù)內(nèi)部屬性和函數(shù)方法實例詳解
函數(shù)是由事件驅(qū)動的或者當(dāng)它被調(diào)用時執(zhí)行的可重復(fù)使用的代碼塊。通過本文給大家介紹JavaScript函數(shù)內(nèi)部屬性和函數(shù)方法,感興趣的朋友一起學(xué)習(xí)吧2016-03-03
讓IDE識別webpack的別名alias的實現(xiàn)方法
這篇文章主要介紹了讓IDE識別webpack的別名alias的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05

