Brief introduction:
AOP (aspect-oriented programming) is a programming idea provided to address the independence and maintainability of functions. When multiple functions re-use the same function in large quantities, the functional balance is divided by slicing, thus improving the low coupling.
JS in the implementation:
Index.html
<type= "Text/javascript" src= "Index.js"></ Script >
Index.js
//Implement call to intercept
Function.prototype.before=function (func) { var _this=this ; return function () { if (func.apply (this , arguments) ===false ) {//execute an AOP function before calling
return
false
;
return _this.apply (
this
,arguments);//execute original Function}}
Implement call after intercept
Function.prototype.after=Function(func) {
var_this= This; return function(){ varResult=_this.apply ( This, arguments);//execute primitive functionif(result===false){ return false; } func.apply ( This, arguments);//execute an AOP function after a callreturnresult; }}
Enabling external functionality through AOPvarhello=function(func) {returnFunc= (function(){ returnFunc.before (function() {Console.log ("Before"); }). After (function() {Console.log ("After"); }); })();}
Main function FunctionfunctionTest () {Console.log ("Test function in Hello function");//Open Debugger}
Replace the main function test=Hello (test);
Run test ();
How JavaScript implements AOP