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)文章
微信小程序使用template標簽實現(xiàn)五星評分功能
這篇文章主要為大家詳細介紹了微信小程序使用template標簽實現(xiàn)五星評分功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
調(diào)用innerHTML之后onclick失效問題的解決方法
調(diào)用innerHTML之后,onclick失效了,這也是在意料之中的,因為innerHTML是以文本形式插入的button,所以無法識別onclick事件2014-01-01
js模仿微信朋友圈計算時間顯示幾天/幾小時/幾分鐘/幾秒之前
本篇文章主要介紹了js模仿微信朋友圈計算時間顯示幾天/幾小時/幾分鐘/幾秒之前的實例。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04
JavaScript實現(xiàn)網(wǎng)頁下拉菜單效果
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)網(wǎng)頁下拉菜單效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-11-11
ES6使用新特性Proxy實現(xiàn)的數(shù)據(jù)綁定功能實例
這篇文章主要介紹了ES6使用新特性Proxy實現(xiàn)的數(shù)據(jù)綁定功能,結(jié)合具體實例形式分析了ES6使用Proxy實現(xiàn)數(shù)據(jù)綁定具體原理、操作步驟與相關(guān)注意事項,需要的朋友可以參考下2020-05-05

