您好,欢迎访问一九零五行业门户网

JavaScript AOP编程实例_javascript技巧

本文实例讲述了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); }});// runtest(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); }});// runmyobj.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程序设计有所帮助。
其它类似信息

推荐信息