How JavaScript implements AOP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.