<script> /** * @author: kanpiaoxue * Description: 模拟Java的StringBuilder */ function StringBuilder(str){ this.arr = []; if(str){ this.arr.push(str); } } StringBuilder.prototype = { append:function(str){ this.arr.push(str); return this; }, toString:function(){ return this.arr.join(''); } }; var builder = new StringBuilder('kanpiaoxue'); var count = 5; for(var i = 0; i < count; i++){ builder.append('hello'+ i); } alert(builder.toString()); //output 'kanpiaoxuehello0hello1hello2hello3hello4' </script>