Last active
August 29, 2015 14:21
-
-
Save Janking/757f1692a6a40b64b2b4 to your computer and use it in GitHub Desktop.
javascript AOP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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