一起运行多个javascript对象方法

时间:2012-11-07 10:26:33

标签: javascript object methods

我正在尝试为我的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$%^"))));

由于

4 个答案:

答案 0 :(得分:3)

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

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/

答案 1 :(得分:1)

你可以扩展String原型:

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());​

答案 2 :(得分:1)

您可以将函数添加到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不支持它。)

答案 3 :(得分:0)

您可能会考虑使用对象的可链接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();