JavaScript AOP编程实例
作者:bea
本文实例讲述了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,
本文实例讲述了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程序设计有所帮助。
有用 | 无用
/*
// 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实现当前页居中分页效果的方法
- javascript中Date()函数在各浏览器中的显示效果
- javaScript中Math()函数注意事项
- 理解Javascript的动态语言特性
- 你所不了解的javascript操作DOM的细节知识点(一)
- jQuery实现复选框批量选择与反选的方法
- 在Python中使用glob模块查找文件路径的方法
- 举例简介AngularJS的内部语言环境
- 详解AngularJS中自定义指令的使用
- 详解AngularJS中的依赖注入机制
- 详解AngularJS中的作用域
- 简介AngularJS中使用factory和service的方法
- 简介AngularJS的视图功能应用
- 在AngularJS中使用AJAX的方法
- 使用AngularJS来实现HTML页面嵌套的方法
- AngularJS的表单使用详解
- 举例讲解AngularJS中的模块
- 简介AngularJS的HTML DOM支持情况
- 在Ubuntu系统上安装Ghost博客平台的教程