JavaScript之AOP編程實(shí)例
本文實(shí)例講述了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è)計(jì)有所幫助。
相關(guān)文章
JavaScript中的事件監(jiān)聽詳細(xì)介紹
這篇文章主要給大家介紹了關(guān)于JavaScript中事件監(jiān)聽的相關(guān)資料,在前端開發(fā)過程中我們經(jīng)常會遇到給頁面元素添加事件的問題,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08
微信小程序使用template標(biāo)簽實(shí)現(xiàn)五星評分功能
這篇文章主要為大家詳細(xì)介紹了微信小程序使用template標(biāo)簽實(shí)現(xiàn)五星評分功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
調(diào)用innerHTML之后onclick失效問題的解決方法
調(diào)用innerHTML之后,onclick失效了,這也是在意料之中的,因?yàn)閕nnerHTML是以文本形式插入的button,所以無法識別onclick事件2014-01-01
js模仿微信朋友圈計(jì)算時(shí)間顯示幾天/幾小時(shí)/幾分鐘/幾秒之前
本篇文章主要介紹了js模仿微信朋友圈計(jì)算時(shí)間顯示幾天/幾小時(shí)/幾分鐘/幾秒之前的實(shí)例。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04
JavaScript實(shí)現(xiàn)網(wǎng)頁下拉菜單效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)網(wǎng)頁下拉菜單效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
ES6使用新特性Proxy實(shí)現(xiàn)的數(shù)據(jù)綁定功能實(shí)例
這篇文章主要介紹了ES6使用新特性Proxy實(shí)現(xiàn)的數(shù)據(jù)綁定功能,結(jié)合具體實(shí)例形式分析了ES6使用Proxy實(shí)現(xiàn)數(shù)據(jù)綁定具體原理、操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-05-05
js控制滾動(dòng)條緩慢滾動(dòng)到頂部實(shí)現(xiàn)代碼
滾動(dòng)條緩慢滾動(dòng)到頂部這樣的效果想必大家在瀏覽網(wǎng)頁的時(shí)候都有見過吧,本文使用js實(shí)現(xiàn)下,感興趣的你可不要錯(cuò)過了哈,希望可以幫助到你2013-03-03

