【问题标题】:Running multiple javascript object methods together一起运行多个 javascript 对象方法
【发布时间】:2012-10-27 09:25:02
【问题描述】:

我正在尝试为我的 ajax 聊天系统编写一个小助手类,我正在努力添加我可能需要的基本功能。

var strings = {
        filterWords: ["fool", "dumb", "arse"],
        removeSpecialChars: function (str) {
            return str.replace(/[^\w\s]/gi, '');
        },
        killSpace: function (str) {
            return str.replace(/\s/g, '');
        },
        reduceSpace: function (str) {
            return str.replace(/\s+/g, ' ');
        },
        allowLetsAndNums: function (str) {
            return str.replace(/[^A-Za-z0-9]/g, ' ');
        },
        allowLets: function (str) {
            return str.replace(/[^A-Za-z]/g, ' ');
        },
        allowNums: function (str) {
            return str.replace(/[^0-9]/g, ' ');
        },
        wordFilter: function (str) {
            var rgx = new RegExp(this.filterWords.join("|"), "gi");
            return str.replace(rgx, "****");
        }
    }

我发现我可能需要同时运行多种方法我在问什么最佳实践可以做到这一点而不会导致以下结果?

alert(strings.wordFilter(strings.reduceSpace(strings.allowLets("efgwge @£235%^@£ fool you a dumb arse432345$%^"))));

谢谢

【问题讨论】:

  • 创建桥接方法来为你做这件事。
  • 尝试拥有一个函数数组,然后遍历每个函数。将上一个结果传递给下一个调用,直到完成。

标签: javascript object methods


【解决方案1】:

你可以让它成为一个流畅的界面,允许这样的代码:

var x = new Validation("efgwge @£235%^@£ fool you a dumb arse432345$%^");
alert(x.allowLets().reduceSpace().wordFilter().result());
// alerts "efgwge **** you a **** ****"

您的主要代码需要是:

var Validation = function(str) {
    this.str = str;
    filterWords = ["fool", "dumb", "arse"]
    this.removeSpecialChars = function () {
        this.str = this.str.replace(/[^\w\s]/gi, '');
        return this;
    };    
    this.killSpace = function () {
        this.str = this.str.replace(/\s/g, '');
        return this;
    };
    this.reduceSpace = function () {
        this.str = this.str.replace(/\s+/g, ' ');
        return this;
    };
    this.allowLetsAndNums = function () {
        this.str = this.str.replace(/[^A-Za-z0-9]/g, ' ');
        return this;
    };
    this.allowLets = function () {
        this.str = this.str.replace(/[^A-Za-z]/g, ' ');
        return this;
    };
    this.allowNums = function () {
        this.str = this.str.replace(/[^0-9]/g, ' ');
        return this;
    };
    this.wordFilter = function () {
        var rgx = new RegExp(filterWords.join("|"), "gi");
        this.str = this.str.replace(rgx, "****");
        return this;
    };
    this.result = function(){
        return this.str;
    };
}

现场示例:http://jsfiddle.net/fb7en/

【讨论】:

  • 真的很喜欢这种方式,这对我来说似乎是最清晰和最干净的,谢谢
【解决方案2】:

你可以扩展字符串原型:

String.prototype.removeSpecialChars = function () {
return this.replace(/[^\w\s]/gi, '');
}
String.prototype.killSpace = function () {
return this.replace(/\s/g, '');
}

var foo = "This is my§$% String";
​document.write​(foo.removeSpecialChars​().killSpace());​

【讨论】:

    【解决方案3】:

    您可以将函数添加到 String.prototype 中,这样您就可以像这样调用函数:

    String.prototype.killSpace = function() {
      return this.replace(/\s/g, '');
    }
    String.prototype.reduceSpace = function () {
      return this.replace(/\s+/g, ' ');
    }
    
    "foo   bar".reduceSpace().killSpace(); // => returns foobar
    

    唯一的缺点是您不能使用 for..in 循环遍历字符串,因为它会将方法列为成员,并且目前没有跨浏览器的方式使其不可迭代(IE不支持)。

    【讨论】:

      【解决方案4】:

      您可以考虑为您的对象使用可链接的 API:

      var StringFilter = {
          _string: '',
          string: function (string) {
              this._string = string || '';
              return this;
          },
          filterWords: ["fool", "dumb", "arse"],
          removeSpecialChars: function () {
              this._string = this._string.replace(/[^\w\s]/gi, '');
              return this;
          },
          killSpace: function () {
              this._string = this._string.replace(/\s/g, '');
              return this;
          },
          reduceSpace: function () {
              this._string = this._string.replace(/\s+/g, ' ');
              return this;
          },
          allowLetsAndNums: function () {
              this._string = this._string.replace(/[^A-Za-z0-9]/g, ' ');
              return this;
          },
          allowLets: function () {
              this._string = this._string.replace(/[^A-Za-z]/g, ' ');
              return this;
          },
          allowNums: function () {
              this._string = this._string.replace(/[^0-9]/g, ' ');
              return this;
          },
          wordFilter: function () {
              var rgx = new RegExp(this.filterWords.join("|"), "gi");
              this._string = this._string.replace(rgx, "****");
              return this;
          },
          select: function () {
              return this._string;
          }
      };
      
      StringFilter
          .string("efgwge @£235%^@£ fool you a dumb arse432345$%^")
          .allowLets()
          .reduceSpace()
          .wordFilter()
          .select();
      

      【讨论】:

      • 好主意,为什么没有想到这个;)
      • 您有一个非常相似的想法,并且由于新对象,您的想法似乎在异步操作中具有优势。 ;)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 2023-03-13
      相关资源
      最近更新 更多