Here's how to use them:
Dog.before('bark',function(){alert('going to bark')});
Dog.after('bark',function(){alert('done barking')});
User.prototype.before('login',function(){alert('logging in!')}The actual code written is very small (20 lines, less if you discount lines taken by braces)
Object.prototype.before = function(func, advice){
var oldfunc = this[func];
this[func] = function(){
advice.apply(this,arguments);
oldfunc.apply(this,arguments);
}
}
Object.prototype.around = function(func, advice){
var oldfunc = this[func];
this[func] = function(){
var myargs = [oldfunc];
for(var i=0; i < arguments.length;i++){
myargs.push(arguments[i])
}
advice.apply(this,myargs);
}
}
Object.prototype.after = function(func, advice){
var oldfunc = this[func];
this[func] = function(){
oldfunc.apply(this,arguments);
advice.apply(this,arguments);
}
}This way you can add any sorts of filters to your JavaScript methods. Enhancing on this is to be able to remove those filters once added and to add a filter to all functions of an object (recursively) at once.We also need some fail safety against users trying to advice non functions or even undefined properties.

2 comments:
Thanks for your posting and have a good week
Making holic gold is the old question : Honestly there is no fast way to make lots of holic money . Sadly enough a lot of the people that all of a sudden come to with millions of holic online gold almost overnight probably duped . Although there are a lot of ways to make lots of cheap holic goldhere I will tell you all of the ways that I know and what I do to make holic online money.
Post a Comment