在JS中实现AOP,都是把一个函数动态的置入到另一个函数里面
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
AOP操作
<script type="text/javascript">
Function.prototype.before = function(beforefn){
var _self = this;
return function(){
beforefn.apply(this,arguments); // 执行before函数
return _self.apply(this,arguments); // 执行本函数
}
};
Function.prototype.after = function(afterfn){
var _self = this;
return function(){
var ret = _self.apply(this,arguments); // 执行本函数
afterfn.apply(this,arguments); // 执行after函数
return ret;
}
};
var func = function(){
console.log('helloWorld!');
}
func = func.before(function(){
console.log("1")
}).after(function(){
console.log("2")
})
func();
</script>
</body>
</html>