栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

JavaScript之AOP编程实例

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaScript之AOP编程实例

本文实例讲述了Javascript之AOP编程。分享给大家供大家参考。具体如下:


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");
  }
});
// 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.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程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/101585.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号