javascript模拟dotNet中的StreamBuild及应用
来源:互联网 发布:专业网络整合推广 编辑:程序博客网 时间:2024/06/06 05:55
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;
}
}
修改后拼接字符串的速度可以提高一个数量级.
- javascript模拟dotNet中的StreamBuild及应用
- javascript中的正则表达式及应用
- Javascript中的闭包及应用
- JavaScript中的DOM简介及应用
- XHTML中的表格应用及表格的模拟
- 面向对象在数据库应用程序中的应用(dotNet)
- 转载:面向对象在数据库应用程序中的应用(dotNet)
- 面向对象在数据库应用程序中的应用(dotNet)
- 浅谈MVC架构在DotNET WinForm中的应用(C#)
- javascript模拟java中的Map
- 模拟strcpy函数及应用
- 模拟函数strcat及应用
- 模拟strstr函数及应用
- DotNet数据库应用
- javascript中的全局变量应用
- Javascript中的DOM应用
- Javascript中的DOM应用
- Java应用中的JavaScript
- System.Windows.Forms.Cursor
- div+css 要点记录
- Ajax - 基础教程第八章例子学习二
- GridView控件中的事件
- 使用Flowplayer(4)
- javascript模拟dotNet中的StreamBuild及应用
- Linux-2.4.x 在 s3c44b0上的移植总结
- 指定S60程序图标的显示位置
- 格式化DataGrid的例子
- 对比sql server 2005 与 oracle
- web.config学习笔记(一)-----节点
- MATLAB Builder for Java 使用初始
- 写出几种常用的排序算法以及执行效率
- C#教程第九课:多态性