<html>
<script>
function StringBuffer(){
this.string = new Array; //创建Array对象存储字符串
}
StringBuffer.prototype.append = function(str){ //把参数str附加到字符串数组
this.string .push(str);
}
StringBuffer.prototype.toString = function(){ //用join方法返回真正的字符串
this.string.join("");
}
var oBuffer = new StringBuffer();
d1 = new Date();
for (var i=0; i < 10000; i++) {
oBuffer.append("text");
}
var sResult = oBuffer.toString();
d2 = new Date();
document.write(d2.getTime()-d1.getTime());
</script>
<body>
</body>
</html>
javaScript 之实现StringBuffer
最新推荐文章于 2018-02-24 09:52:17 发布
本文介绍了一种使用JavaScript实现字符串缓冲区的方法。通过创建Array对象来存储字符串,并提供append方法进行字符串追加,最后利用join方法将数组转换为完整的字符串。通过这种方式可以有效地管理大量字符串的拼接操作。
985

被折叠的 条评论
为什么被折叠?



