jquery AOP:使用相同的建议为相同的方法名称定义多个目标

jquery AOP:Define many targets for the same method name with the same advise

本文关键字:定义 目标 方法 AOP jquery      更新时间:2023-09-26

>我使用AOP Jquery,其许可如下:

/**
* jQuery AOP - jQuery plugin to add features of aspect-oriented programming (AOP) to jQuery.
* http://jquery-aop.googlecode.com/
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Version: 1.3
*
* Cross-frame type detection based on Daniel Steigerwald's code (http://daniel.steigerwald.cz)
* http://gist.github.com/204554
*
*/

当我在这些目标中具有相同的方法名称时,我可以定义许多目标吗:

例:

  var Person={
       doctor:{
          setResume:function(){
           }  
        },
        engineer:{
          setResume:function(){
           }  
        }
}

而不是:

$.aop.before({
    target:Person.doctor,
    method:'setResume'},function(args) {
        console.log('setResume will be called '):
    }
});
$.aop.before({
    target:Person.engineer,
    method:'setResume'},function(args) {
        console.log('setResume will be called '):
    }
});

我想要例如:

$.aop.before({
    target:[Person.doctor,Person.engineer],
// or target :'Person.*' ...
    method:'setResume'},function(args) {
        console.log('setResume will be called '):
    }
});

基于对jQuery AOP源代码的快速回顾,看起来,不,这行不通。但是,我不明白为什么您不能创建一个补丁来允许该行为,或者向所有者建议该功能,或者只是创建自己的帮助程序函数。 像这样的东西(未经测试,将其视为伪代码):

$.aop.beforeMultiple = function(targets, method) {
    for(var i =0;i<targets.length;i++)
        $.aop.before(targets[i], method);
}

如果,它看起来像,不,正如@mgroves所说,安全方法是:

$.aop.beforeMultiple = function() {
    var targets=arguments[0].target;
    for(var i =0;i<targets.length;i++)
     {
        arguments[0].target=targets[i];    
        $.aop.before.call(this, arguments);
     }
}

因为$.aop.before的参数是:

= Arg1:一个对象:{target:Aaaa,method:'yyy'}

=Arg2 : 一个函数 : function(args){}