javascript模拟dotNet中的StreamBuild及应用

     javascript做为弱类型的编程语言,并不具备真正的面向对象特性.而在字符串的拼接过程中,每一次+=的操作都伴随着两次对象的初始化和销毁.不仅速度慢,而且效率也不高.通过javascript内置的array对象和prototype属性模拟出StringBuilder类.

//=============================================================================
//class StringBuilder:字符串拼接
//=============================================================================
 

 StringBuilder=function()
{
 this.buffer=null;
 this.buffer=new Array();
 StringBuilder.prototype.Append=function append(string)
 {
  if((string==null)||(typeof(string)=='undefined'))
  {
   return;
  }
  if((typeof(string)=='string')&&(string.length==0))
  {
   return;
  }
  this.buffer.push(string);
 }
 StringBuilder.prototype.AppendLine=function appendLine(string)
 {
  this.Append(string);
  this.buffer.push("/r/n");
 }
 StringBuilder.prototype.Clear=function clear()
 {
  if(this.buffer.length>0)
  {
   this.buffer.splice(0,this.buffer.length);
  }
 }
 StringBuilder.prototype.IsEmpty=function isEmpty()
 {
  return (this.buffer.length==0);
 }
 StringBuilder.prototype.ToString=function toString()
 {
  return this.buffer.join("");
 }
}

下面是基于StreamBuilder生成表格的TableBuilder类

//=============================================================================
//class TableBuilder类:生成表格
//=============================================================================

TableBuilder=function(heigth,witdh)
{
 this.otable=null;
 this.otable=new StringBuilder();
 this.otable.Append("<table height="+heigth+" width="+width+">");
 TableBuilder.prototype.AppendInnerTD=function appendInnerTD(string)
 {
  if((string==null)||(typeof(string)=='undefined'))
  {
   return;
  }
  if((typeof(string)=='string')&&(string.length==0))
  {
   return;
  }
  this.otable.Append("<td>");
  this.otable.Append(string);
  this.otable.Append("</td>");
 }
 TableBuilder.prototype.AppendOutTD=function appendOutTD(string)
 {
  if((string==null)||(typeof(string)=='undefined'))
  {
   return;
  }
  if((typeof(string)=='string')&&(string.length==0))
  {
   return;
  }
  this.otable.Append(string);
 }
 TableBuilder.prototype.AppendInnerTR=function appendInnerTR(string)
 {
  if((string==null)||(typeof(string)=='undefined'))
  {
   return;
  }
  if((typeof(string)=='string')&&(string.length==0))
  {
   return;
  }
  this.otable.Append("<tr>");
  this.otable.Append(string);
  this.otable.Append("</tr>");
 }
 TableBuilder.prototype.AppendOutTR=function appendOutTR(string)
 {
  if((string==null)||(typeof(string)=='undefined'))
  {
   return;
  }
  if((typeof(string)=='string')&&(string.length==0))
  {
   return;
  }
  this.otable.Append(string);
 }
    TableBuilder.prototype.GetTableHtml()
    {
  this.otable.Append("</table>");
  return this.otable;
    } 
}

修改后拼接字符串的速度可以提高一个数量级.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值