面向?qū)ο蟮腏avaScript (一、對(duì)象基礎(chǔ),使用函數(shù)來模擬類)
通過一個(gè)例子可以很直觀的表述本主題,所以沒有任何其他的說明文字,請(qǐng)看代碼吧。 1
function Person(name, age) {
2
3
//私有變量(private variables)
4
var myName = name;
5
var myAge = age;
6
7
//公共屬性(public properties)
8
this.name = myName;
9
this.age = myAge;
10
11
//私有函數(shù)(private functions)
12
function getMyName() {
13
return myName;
14
};
15
16
function getName() {
17
return name;
18
};
19
20
var getMyAge = function () {
21
return myAge;
22
};
23
24
var getAge = function () {
25
return age;
26
};
27
28
//特權(quán)方法(privileged methods)
29
this.getName = function () {
30
return this.name;
31
//return myName; 可以訪問私有變量
32
//return name; 可以訪問構(gòu)造函數(shù)參數(shù)(其參數(shù)本質(zhì)也是私有變量)
33
//return getMyName(); 可以訪問私有函數(shù)
34
//return getName(); 可以訪問私有函數(shù)
35
};
36
37
this.getAge = function () {
38
return getAge();
39
};
40
41
this.getRealAge = function () {
42
return this.age;
43
};
44
}
45
46
//公共方法(public methods)
47
Person.prototype.acceptName = function (param) {
48
this.name = param;
49
//name = param; 公共方法不能訪問私有變量
50
//myName = param; 公共方法不能訪問私有變量
51
};
52
53
//原型屬性(prototype properties)
54
Person.prototype.legs = 2;
55
56
//靜態(tài)屬性(static properties)
57
//整個(gè)類只有一個(gè)靜態(tài)屬性,各個(gè)對(duì)象共享同一個(gè)靜態(tài)屬性值
58
Person.arms = 2;
59
60
Person.getArms = function () {
61
return Person.arms;
62
//return this.arms; 可以使用this來指代Person對(duì)象本身
63
};
64
65
/*
66
Person是一個(gè)邏輯上的類,他的本質(zhì)是一個(gè)Function,在JavaScript中類是由Function來模擬的,所以Person還是一個(gè)Function的實(shí)例。而arms和getArms方法是一個(gè)Function對(duì)象實(shí)例上的屬性和方法,是Person對(duì)象所特有的,所以本例可以使用Person.arms來引用,同時(shí)在getArms方法中,可以使用this.arms來引用,因?yàn)間etArms是Person對(duì)象上的方法,在該方法內(nèi)部this指代Person對(duì)象自身。
67
*/
68
alert(Person.getArms());
69
70
var pa = new Person("Teddy", 25);
71
72
//alert(pa.myName); 不能訪問私有變量
73
74
alert(pa.name);
75
76
alert(pa.getAge());
77
78
pa.acceptName("Born");
79
80
alert(pa.getName());
81
82
var pb = new Person("John", 18);
83
84
//運(yùn)行時(shí)動(dòng)態(tài)加入方法
85
Person.prototype.acceptAge = function (param) {
86
this.age = param;
87
};
88
89
//pb對(duì)象也可以調(diào)用動(dòng)態(tài)添加的方法
90
pb.acceptAge(30);
91
92
alert(pb.getAge());
93
94
alert(pb.getRealAge());
95
96
//運(yùn)行時(shí)覆蓋已定義方法
97
Person.prototype.acceptName = function (param) {
98
return param;
99
};
100
101
//所有已創(chuàng)建對(duì)象都自動(dòng)繼承被覆蓋的方法
102
alert(pa.acceptName("Black"));
103
104
alert(pa.getName());
105
106
alert(pa.legs);
107
108
//運(yùn)行時(shí)改變?cè)蛯傩灾?/span>
109
Person.prototype.legs = 4;
110
111
//所有已創(chuàng)建對(duì)象都自動(dòng)繼承被改變的原型屬性值
112
alert(pb.legs);
113
114
//運(yùn)行時(shí)將原型屬性改為公共方法
115
Person.prototype.legs = function () {
116
return 4;
117
};
118
119
//所有已創(chuàng)建對(duì)象都自動(dòng)繼承原型屬性到公共方法的改變
120
alert(pa.legs());

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

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

posted on 2008-09-03 19:09 Eric Song 閱讀(1285) 評(píng)論(1) 編輯 收藏 所屬分類: JavaScript