javascript AOP 实现ajax回调函数使用比较方便
javascript AOP 实现ajax回调函数使用比较方便
发布时间:2016-12-30 来源:查字典编辑
摘要:复制代码代码如下:functionactsAsDecorator(object){object.setupDecoratorFor=func...

复制代码 代码如下:

function actsAsDecorator(object) {

object.setupDecoratorFor = function(method) {

if (! ('original_' + method in object) ) {

object['original_' + method] = object[method];

object['before_' + method] = [ ];

object['after_' + method] = [ ];

object[method] = function() {

var i;

var b = this['before_' + method];

var a = this['after_' + method];

var rv;

for (i = 0; i < b.length; i++) {

b[i].call(this, arguments);

}

rv = this['original_' + method].apply(this, arguments);

for (i = 0; i < a.length; i++) {

a[i].call(this, arguments);

}

return rv;

}

}

};

object.before = function(method, f) {

object.setupDecoratorFor(method);

object['before_' + method].unshift(f);

};

object.after = function(method, f) {

object.setupDecoratorFor(method);

object['after_' + method].push(f);

};

}

/**

Invoking

*/

function Test(){

this.say1 = function(s){

alert(s);

}

this.say2 = function(s){

alert(s);

}

}

var t = new Test();

actsAsDecorator(t);

t.before("say1",beforeHander);

t.after("say2",afterHander);

test();

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Javascript教程学习
热门Javascript教程学习
编程开发子分类