---
title: "JS实现类似Java的StringBuffer类"
author: "ganglong99"
date: "2010-07-05T21:45:25Z"
tags: []
categories: ["Web前端"]
slug: "js-java-stringbuffer"
draft: false
lastmod: "2010-07-05T21:48:37Z"
id: 706177
url: "https://it-tech.iteye.com/blog/706177"
---

实现了一个类似Java中StringBuffer的append方法，进行字符串连接：

```
<script language="JavaScript">
  <!--
	
function StringBuffer(){
	this.bb = [];
}
StringBuffer.prototype.append = function(str){
	this.bb.push(str);
	return this;

}
StringBuffer.prototype.toString = function(){
	return this.bb.join("");
}

// 测试代码
var sb= new StringBuffer();
sb.append("hello").append(", ").append('world').append("!");
document.write(sb.toString());

 //-->
 </script>
```

 

输出结果为：hello, world!