国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript AOP編程實例

2019-11-20 12:13:54
字體:
供稿:網(wǎng)友

本文實例講述了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程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 兴宁市| 合川市| 孟连| 泰兴市| 广西| 大同市| 广汉市| 惠东县| 长葛市| 康平县| 上思县| 湘阴县| 比如县| 华池县| 新疆| 五峰| 余姚市| 天镇县| 元氏县| 济宁市| 孟村| 施甸县| 平湖市| 闵行区| 琼结县| 石渠县| 奉新县| 太康县| 武威市| 连江县| 丰宁| 贵德县| 额济纳旗| 耒阳市| 庆云县| 景宁| 财经| 依兰县| 高安市| 衡东县| 重庆市|