下面小编就为大家带来一篇浅谈js中StringBuffer类的实现方法及使用。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
如下所示:
JAVA中有一个StringBuffer类,js中却没有下面来自己实现一个简单的js的StringBuffer类。 //创建一个StringBuffer类 ,此类有两个方法:一个是append方法一个是toString方法 function StringBuffer() { this.__strings__ = []; }; StringBuffer.prototype.append = function(str) { this.__strings__.push(str); }; StringBuffer.prototype.toString = function() { return this.__strings__.join(''); };
StringBuffer的使用:
//此实例的主要是用来拼接字符串
function GnComCard(companymsg) { var buffer = new StringBuffer();//声明一个对象 buffer.append('
'); buffer.append('
'+ companymsg.companyMasterName+ '' + FormatDate(companymsg.crtime) + '
'); buffer.append(''); if (companymsg.newsId != null) { buffer.append(''+ companymsg.newsTitle + ''); } else { buffer.append(''+ companymsg.newsTitle + ''); } if (companymsg.newsContent != null) { buffer.append('
'); buffer.append('>>展开
'); buffer.append(''+ companymsg.newsContent + '
'); } buffer.append(''); buffer.append('