时间:2021-09-14 15:04:26 | 栏目:JavaScript代码 | 点击:次
实现:
function StringBuffer() { this.__strings__ = []; }; StringBuffer.prototype.Append = function (str) { this.__strings__.push(str); return this; }; //格式化字符串 StringBuffer.prototype.AppendFormat = function (str) { for (var i = 1; i < arguments.length; i++) { var parent = "\\{" + (i - 1) + "\\}"; var reg = new RegExp(parent, "g") str = str.replace(reg, arguments[i]); } this.__strings__.push(str); return this; } StringBuffer.prototype.ToString = function () { return this.__strings__.join(''); }; StringBuffer.prototype.clear = function () { this.__strings__ = []; } StringBuffer.prototype.size = function () { return this.__strings__.length; }
实例化调用
var sbHtml=new StringBuffer(); sbHtml.Append('hello'); sbHtml.Append('world'); console.log(sbHtml.ToString());