问题描述
我正在构建一个简单的验证库,并且需要使用Proxy,因为我想接受自定义验证规则作为链对象。我构建了一些东西,它在现代浏览器上正常工作,但在IE11上却不工作,我尝试使用proxy-polyfill但它也不能正常工作。我的代理代码如下。
function contextProxy(context) {
return new Proxy(context,{
get(obj,prop) {
if (prop in obj) {
return obj[prop];
}
const newContext = contextProxy(context._clone());
if (definedRules.hasOwnProperty(prop)) {
return newContext._takeRule(definedRules[prop]);
}
if (customrules.hasOwnProperty(prop)) {
return newContext._takeRule(customrules[prop]);
}
},});
}
我使用该代理;
function validationL() {
return contextProxy(new ValidationLContext());
}
我已经定义了Rules对象;
const definedRules = {
numeric: function () {
return function (text) {
return /^\d+$/.test(text);
};
},lowercase: function () {
return function (text) {
return /^([a-z]+\s*)+$/.test(text);
};
},uppercase: function () {
return function (text) {
return /^([A-Z]+\s*)+$/.test(text);
};
},minLength: function (min) {
return function (text) {
return text.length >= min;
};
},maxLength: function (max) {
return function (text) {
return text.length <= max;
};
},alphaNumeric: function () {
return function (text) {
return /^([a-zA-Z0-9 _-]+)$/i.test(text);
};
},specialChars: function () {
return function (text) {
return !/^([a-zA-Z0-9 _-]+)$/i.test(text);
};
},email: function () {
return function (text) {
return /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(
text
);
};
}
};
ValidationLContext.js
function ValidationLContext(isNot = false,chain = []) {
this.chain = chain;
this.isNot = isNot;
}
ValidationLContext.prototype.not = function () {
this.isNot = true;
return this;
};
ValidationLContext.prototype._takeRule = function (ruleFn) {
return (...args) => {
this.chain.push({ fn: ruleFn.apply(this,args),isNot: this.isNot });
if (this.isNot) {
this.isNot = false;
}
return this;
};
};
ValidationLContext.prototype.validate = function (text) {
return this.chain.every((c) =>
c.isNot ? !c.fn.call(this,text) : c.fn.call(this,text)
);
};
ValidationLContext.prototype._clone = function () {
return new ValidationLContext(this.isNot,this.chain);
};
export default ValidationLContext;
所以图书馆的使用是这样的
validationL().numeric().minLength(3).validate("123");
validationL().not().numeric().minLength(3).validate("123");
我可以在像Chrome这样的现代浏览器中像上面那样使用,但是当我尝试使用IE11时,not()函数只能工作,因此只有对象函数可以工作。 谁能帮我这个忙。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)