<!DOCTYPE html>
<title>hello world</title>
<script>
Function.prototype.before = function(beforefn){
var _self = this; // 儲存原函數的引用
return function(){// 傳回包含了原函數和新函數的代理函數
beforefn.apply(this,arguments);// 執行新函數
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); // 執行新函數
return ret;
};
}
var func = function(){
alert(2);
};
func = func.before(function(){
alert(1);
}).after(function(){
alert(3);
});
window.onload = function(){
func();
};
</script>