装饰器作用: 不改变原有的结构和功能,为对象添加新的功能或者增强原有的功能。
let f1 = function() {
console.log('原函数')
};
let f2 = function() {
console.log('装饰函数1')
};
let f3 = function() {
console.log('装饰函数2')
};
// 普通装饰器
Function.prototype.Director = function(...fns) {
this();
fns.forEach((item)=>{
item();
})
}
f1.Director(f2, f3); //原函数 装饰函数1 装饰函数2
// 装饰器链版本
Function.prototype.DirectorChain = function(fn) {
let _this = this;
return function() {
_this();
fn();
}
}
f1.DirectorChain(f2).DirectorChain(f3)(); //原函数 装饰函数1 装饰函数2