Javascript AOP库 [英] Javascript AOP libraries

查看:152
本文介绍了Javascript AOP库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你使用哪个Javascript AOP库,它的主要功能是什么?

Which Javascript AOP library do you use, and what are its key features ?

推荐答案

这是我到现在为止所发现的:

Here is what I found until now :


  • dotvoid 的实现,干净的语法,很好用,文章很好地介绍了为什么/如何使用给定的代码,支持介绍,但是被窃听,

  • dojox 此处是一个很好的介绍如何使用它,

  • 有一个jQuery插件, jquery-aop ,语法粗糙,在javascript对象中传递对象和方法,

  • AspectJS ,语法更加粗糙(需要将切入点类型作为参数传递给单个方法)

  • dotvoid's implementation, clean syntax, nice to use, the article is a good introduction on why/how to use the given code, supports introductions, but is bugged,
  • Dojo has what seems to be a good built-in implementation in dojox, here is a nice introduction on how to use it,
  • there is a plugin for jQuery, jquery-aop, with a rougher syntax, passing objects and methods in a javascript object,
  • AspectJS with an even rougher syntax (need to pass type of pointcut as arguments to a single method)

就像我说的那样,dotvoid的代码不起作用。
我稍微纠正了一些似乎更好的东西:

Like I said, dotvoid's code did not work. I corrected a little and got something that seems to work better :

InvalidAspect = new Error("Missing a valid aspect. Aspect is not a function.");
InvalidObject = new Error("Missing valid object or an array of valid objects.");
InvalidMethod = new Error("Missing valid method to apply aspect on.");

function doBefore(beforeFunc,func){
    return function(){
        beforeFunc.apply(this,arguments);
        return func.apply(this,arguments);
    };  
}

function doAfter(func, afterFunc){
    return function(){
        var res = func.apply(this,arguments);
        afterFunc.apply(this,arguments);
        return res;   
    };
}

Aspects = function(){};
Aspects.prototype={
    _addIntroduction : function(intro, obj){
         for (var m in intro.prototype) {
              obj.prototype[m] = intro.prototype[m];
            }
        },

    addIntroduction : function(aspect, objs){
        var oType = typeof(objs);

        if (typeof(aspect) != 'function')
        throw(InvalidAspect);

        if (oType == 'function'){
            this._addIntroduction(aspect, objs);
        }
        else if (oType == 'object'){
            for (var n = 0; n < objs.length; n++){
                this._addIntroduction(aspect, objs[n]);
            }
        }
        else{
            throw InvalidObject;
        }
    },

    addBefore : function(aspect, obj, funcs){
          var fType = typeof(funcs);

          if (typeof(aspect) != 'function')
            throw(InvalidAspect);

          if (fType != 'object')
            funcs = Array(funcs);

          for (var n = 0; n < funcs.length; n++){
            var fName = funcs[n];
            var old = obj.prototype[fName];

            if (!old)
              throw InvalidMethod;

            var res = doBefore(aspect,old)
            obj.prototype[fName] = res;
        }
    },

    addAfter : function(aspect, obj, funcs) {
          if (typeof(aspect) != 'function')
            throw InvalidAspect;

          if (typeof(funcs) != 'object')
            funcs = Array(funcs);

          for (var n = 0; n < funcs.length; n++)
          {
            var fName = funcs[n];
            var old = obj.prototype[fName];

            if (!old)
              throw InvalidMethod;

            var res = doAfter(old,aspect);
            obj.prototype[fName] = res;
          }
        },

    addAround : function(aspect, obj, funcs){
          if (typeof(aspect) != 'function')
            throw InvalidAspect;

          if (typeof(funcs) != 'object')
            funcs = Array(funcs);

          for (var n = 0; n < funcs.length; n++)
          {
            var fName = funcs[n];
            var old = obj.prototype[fName];
            if (!old)
              throw InvalidMethod;

            var res = aspect(old);
            obj.prototype[fName] = res;
          }

          return true;
        }
}

这篇关于Javascript AOP库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆