// 维护权限列表
const jobList = ['FE', 'BE'];
// 策略
var strategies = {
checkRole: function(value) {
return value === 'juejin';
},
checkGrade: function(value) {
return value >= 1;
},
checkJob: function(value) {
return jobList.indexOf(value) > 1;
},
checkEatType: function(value) {
return value === 'eat melons';
}
};
// 校验规则
var Validator = function() {
this.cache = [];
// 添加策略事件
this.add = function(value, method) {
console.log(value,'-----1')
console.log(strategies[method](value),'-----2')
this.cache.push(function() {
return strategies[method](value);
});
};
// 检查
this.check = function() {
console.log(this.cache)
for (let i = 0; i < this.cache.length; i++) {
let valiFn = this.cache[i];
console.log(valiFn)
var data = valiFn(); // 开始检查
if (!data) {
return false;
}
}
return true;
};
};
// 小彭使用策略模式进行操作
var compose1 = function() {
var validator = new Validator();
console.log(validator,'validator')
const data1 = {
role: 'juejin',
grade: 3
};
validator.add(data1.role, 'checkRole');
validator.add(data1.grade, 'checkGrade');
const result = validator.check();
console.log(result,'result')
return result;
};
compose1()
console