Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript设计模式之装饰者模式 #65

Open
pengjielee opened this issue Oct 31, 2019 · 0 comments
Open

Javascript设计模式之装饰者模式 #65

pengjielee opened this issue Oct 31, 2019 · 0 comments

Comments

@pengjielee
Copy link
Owner

装饰者模式可以动态地给某个对象添加一些额外的职责,而不会影响从这个类中派生的其他对象。

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 result = self.apply(this,arguments);
		afterFn.apply(this,arguments);
		return result;
	}
}

数据统计

var login = function(){
	console.log('login')
}

var log = function(){
	console.log('user login')
}

login = login.after(log);

login();

改变参数

var login = function(type,url,param){
	console.log(param)
}

var getToken = function(){
	return 'token'
}

login = login.before(function(type,url,param){
	param.token = getToken();
})

login('get','http://baidu.com',{ name: 'hello' });

表单验证

var submit = function(){
	console.log('submit form')
}

var validate = function(){
	console.log('validate form')
}

submit = submit.before(validate);

submit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant