JavaScript之AOP編程執行個體,aop編程執行個體

來源:互聯網
上載者:User

JavaScript之AOP編程執行個體,aop編程執行個體

本文執行個體講述了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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.