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>實(shí)例</title>
6
<script type="text/javascript">
7
/*
8
* 項(xiàng)目: book -> Javascript高級程序設(shè)計(jì).pdf -> 第3章 -> 3.5.8實(shí)例
9
*
10
* 說明:自定義對象的使用
11
*
12
* 練習(xí)者: Alex刺客
13
*
14
* 日期: 2009-12-13
15
*/
16
17
/*
18
定義一個(gè)一次合并當(dāng)前對象所有字符串的StringBuffer類
19
*/
20
21
function StringBuffer () {
22
this._strings_ = new Array;
23
if (typeof StringBuffer._initialized == "undefined") {
24
StringBuffer.prototype.append = function (str){
25
this._strings_.push(str);
26
}
27
StringBuffer.prototype.toString = function(){
28
return this._strings_.join("");
29
}
30
}
31
}
32
33
var stringBufferTest = new StringBuffer();
34
var string2 = new StringBuffer();
35
stringBufferTest.append("Hello ");
36
stringBufferTest.append("World! ");
37
stringBufferTest.append("Welcome");
38
stringBufferTest.append("to ");
39
stringBufferTest.append("JavaScript! ");
40
string2.append("Alex ");
41
string2.append("刺客!");
42
var result = stringBufferTest.toString();
43
var test = string2.toString();
44
alert(result);
45
alert(test);
46
</script>
47
</head>
48
<body>
49
</body>
50
</html>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50
