JavaScript实现一个StringBuffer

/**

 * 模拟StringBuffer

 * 使用过程基本上和java.lang.StringBuffer一样,但是在toString中可以插入风格符,默认分隔符为','

 * */

function StringBuffer() {
    this.strs = []; //用数组来缓存所有的内容

    if (typeof StringBuffer._initialized == "undefined") {
        StringBuffer._initalized = true; //已经初始化,无需再初始化

        StringBuffer.prototype.append = function(str) {
            this.strs.push(str);
            return this;
        };

        StringBuffer.prototype.toString = function(seperator) {
            if (!seperator) {
                seperator = "";
            }
            return this.strs.join(seperator);
        };
    }
}

猜你喜欢

转载自blog.csdn.net/brianwey/article/details/79358849