The examples in this article describe JavaScript AOP programming. Share to everyone for your reference. as follows:
*//AOP ({options}); by:adamchow2326@yahoo.com.au//version:1.0/Simple aspect oriented programming module//support aspect-before, AF
ter and around//USAGE:AOP ({context:myobject,//scope context of the target function.) Target: "Test",//target function name Before:function () {//Before function would be run before target F
Unction Console.log ("AOP before");
}, After:function () {//After function would be run after the target function Console.log ("AOP after"); }, Around:function () {//around function would be run before and after the target function Console.lo
G ("AOP around");
}
}); * * var AOP = (function () {var options = {}, context = window, ofn, Ofnarg, TARGETFN, Targetfnselect Or, Beforefn, AFTERFN, AROUNDFN, clonefn = function (fn) {if (typeof Fn = = "function") {RE
Turn 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; The Run = function () {Context[targetfnselector] = function (Ofnarg) {if (AROUNDFN) {Arou
Ndfn.apply (this, arguments); } if (Beforefn) {beforefn.apply (this, arguments);//' The ' 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.lo
G ("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.A
GE);
}, After:function () {Console.log ("AOP after say name =" + This.age);
}, Around:function () {console.log ("AOP around say name =" + This.age);
}
});
Myobj.childObj.say ();
The
wants this article to help you with your JavaScript programming.