js 裝飾模式

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title></title>
</head>
<body>
	<input type="button"  id="button" value='putton'>
</body>
</html>
<script>
	
	var count=0;

	Function.prototype.before =function(fn){
		var _self = this;
		return function(){
			fn.apply(this,arguments);
			return _self.apply(this,arguments);
		}
	}

	Function.prototype.after=function(fn){
		var _self = this;
		return function(){
			var ret  = _self.apply(this,arguments);
			fn.apply(this,arguments);	
			return ret;
		}
	}

	var beforeClick = function(){
		if(!count){
			console.log('this is first click');
		}else{
			console.log('this is '+ count +' click');
		}
        count++;
	}


	var  clickFun = function(){
		console.log('this is  orgran function ');
	}

	var log = function(){
		console.log(' this is  upload logs');
	}

	document.getElementById('button').onclick =  clickFun.before(beforeClick).after(log);

</script>
相關文章
相關標籤/搜索