ltre.js
Version:
Personal Javascript Libs Store
1 lines • 1.66 kB
JavaScript
//注入支持
function inject(obj, map){
//map: key, onStart:{callback, args}, onEnd:{callback, args}
for (var i in obj) {
if (i in map) {
var _hook = obj[i];
var startCb = typeof map[i].onStart.callback == "function" ? map[i].onStart.callback : function(){};
var endCb = typeof map[i].onEnd.callback == "function" ? map[i].onEnd.callback : function(){};
var startCbArgs = map[i].onStart.args || [];
var endCbArgs = map[i].onEnd.args || [];
~function(_hook, startCb, endCb, startCbArgs, endCbArgs){
obj[i] = function(){
startCb.apply(this, startCbArgs);
_hook.apply(this, arguments);
endCb.apply(this, endCbArgs);
};
}(_hook, startCb, endCb, startCbArgs, endCbArgs);
}
}
}
//实例
function Example(){
this.sb = function(a, b){
alert(a + b);
};
this.hehe = function(){
alert('null');
};
}
var j = new Example();
inject(j, {
sb:{
onStart:{
callback:function(a,b){
alert(a+b)
},
args:[1,2]
},
onEnd:{
callback:function(c,d){
alert(c+d)
},
args:[3,4]
}
},
hehe:{
onStart:{
callback:function(e,f){
alert(e+f)
},
args:[5,6]
},
onEnd:{
callback:function(g,h){
alert(g+h)
},
args:[7,8]
}
}
});
//测试
j.sb(8,9);
j.hehe(5,5);