1
<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
2
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
3
<
head
>
4
<
meta http
-
equiv
=
"
Content-Type
"
content
=
"
text/html; charset=utf-8
"
/
>
5
<
title
><
/
title>
6
<
/
head>
7
<
body
>
8
<
/
body>
9
<
script type
=
"
text/javascript
"
><!--
10
var
str
=
'
hello
'
;
11
str
+=
'
world
'
;
12
//
每次完成字符串连接都会执行步骤2到6步
13
//
实际上,这段代码在幕后执行的步骤如下:
14
/*
15
1.创建存储'hello'的字符串
16
2.创建存储'world'的字符串
17
3.创建存储链接结果的字符串
18
4.把str的当前内容复制到结果中
19
5.把'world'复制到结果中
20
6.更新str,使它指向结果
21
*/
22
23
//
为了提高性能最好使用数组方法拼接字符串
24
//
创建一个StringBuffer类
25
function
StringBuffer()
{
26
this.__strings__ = [];
27
}
;
28
StringBuffer.prototype.append
=
function
(str)
{
29
this.__strings__.push(str);
30
}
;
31
StringBuffer.prototype.toString
=
function
()
{
32
return this.__strings__.join('');
33
}
;
34
35
//
调用StringBuffer类,实现拼接字符串
36
//
每次完成字符串连接都会执行步骤2步
37
//
实际上,这段代码在幕后执行的步骤如下:
38
/*
39
1.创建存储结果的字符串
40
2.把每个字符串复制到结果中的合适位置
41
*/
42
var
buffer
=
new
StringBuffer();
43
buffer.append(
'
hello
'
);
44
buffer.append(
'
world
'
);
45
var
result
=
buffer.toString();
46
47
//
用StringBuffer类比使用+=节省50%~66%的时间
48
//
-->
49
<
/
script>
50
<
/
html>
51
<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
2
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
3
<
head
>
4
<
meta http
-
equiv
=
"
Content-Type
"
content
=
"
text/html; charset=utf-8
"
/
>
5
<
title
><
/
title>
6
<
/
head>
7
<
body
>
8
<
/
body>
9
<
script type
=
"
text/javascript
"
><!--
10
var
str
=
'
hello
'
;11
str
+=
'
world
'
;12
//
每次完成字符串连接都会执行步骤2到6步
13
//
实际上,这段代码在幕后执行的步骤如下:
14

/*15
1.创建存储'hello'的字符串16
2.创建存储'world'的字符串17
3.创建存储链接结果的字符串18
4.把str的当前内容复制到结果中19
5.把'world'复制到结果中20
6.更新str,使它指向结果21
*/
22
23
//
为了提高性能最好使用数组方法拼接字符串
24
//
创建一个StringBuffer类
25

function
StringBuffer()
{26
this.__strings__ = [];27
}
; 28

StringBuffer.prototype.append
=
function
(str)
{29
this.__strings__.push(str);30
}
;31

StringBuffer.prototype.toString
=
function
()
{32
return this.__strings__.join('');33
}
;34
35
//
调用StringBuffer类,实现拼接字符串
36
//
每次完成字符串连接都会执行步骤2步
37
//
实际上,这段代码在幕后执行的步骤如下:
38

/*39
1.创建存储结果的字符串40
2.把每个字符串复制到结果中的合适位置41
*/
42
var
buffer
=
new
StringBuffer();43
buffer.append(
'
hello
'
);44
buffer.append(
'
world
'
);45
var
result
=
buffer.toString();46
47
//
用StringBuffer类比使用+=节省50%~66%的时间
48
//
-->
49
<
/
script>
50
<
/
html>
51
