微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JavaScript代理在ie11上无法正常工作

如何解决JavaScript代理在ie11上无法正常工作

我正在构建一个简单的验证库,并且需要使用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 举报,一经查实,本站将立刻删除。