Skip to content

Instantly share code, notes, and snippets.

@Janking
Last active August 29, 2015 14:21
Show Gist options
  • Save Janking/757f1692a6a40b64b2b4 to your computer and use it in GitHub Desktop.
Save Janking/757f1692a6a40b64b2b4 to your computer and use it in GitHub Desktop.
javascript AOP
<script type="text/javascript">
//所谓AOP
//在执行B前先执行A,
//执行完B后再执行C
Function.prototype.before = function(beforefn){
var __self = this;
console.log(__self);
return function(){
beforefn.apply(this,arguments);
return __self.apply(this,arguments);
}
}
Function.prototype.after = function(afterfn){
var __self = this;
/*
this => __self => function(){
beforefn.apply(this,arguments);
return __self.apply(this,arguments);
}
*/
return function(){
var ret = __self.apply(this,arguments);
afterfn.apply(this,arguments);
return ret;
}
}
var func = function(){
console.log('B');
}
func = func.before(function(){
console.log('A')
}).after(function(){
console.log('C')
});
func()
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment