JavaScript AOP Programming Instance _javascript tips

Source: Internet
Author: User
Tags aop

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.

Related Article

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.