Code
1 <script type="text/javascript">
2 //------------------对象冒充实现继承-----------------
3 function ClassA(sColor)
4 {
5 this.color=sColor;
6 this.showColor=function()
7 {
8 alert(this.color);
9 }
10 }
11 function ClassC(iBig)
12 {
13 this.color=iBig;
14 this.showBig=function()
15 {
16 alert(this.color);
17 }
18
19 }
20 function ClassB(sColor,sName,iBig)
21 {
22 this.newMethed=ClassA;
23 this.newMethed(sColor);
24 delete this.newMethed;
25
26 this.newMethed=ClassC;
27 this.newMethed(iBig);
28 delete this.newMethed;
29
30 this.name=sName;
31 this.sayName=function()
32 {
33 alert(this.name);
34 }
35 }
36 //var oCarA=new ClassA("red");
37 //oCarA.showColor();
38 var oCarB=new ClassB("blue","myTest",100);
39 oCarB.showColor();
40 oCarB.showBig();
41 oCarB.sayName();
42 //----------------call或apply方法实现继承---------------------
43 function ClassA(sColor)
44 {
45 this.color=sColor;
46 this.showColor=function()
47 {
48 alert(this.color);
49 }
50 }
51 function ClassB(sColor,sName)
52 {
53 //this.newMethed=ClassA;
54 //this.newMethed(sColor);
55 //delete this.newMethed;
56 //ClassA.call(this,sColor);
57 //ClassA.apply(this,new Array(sColor));
58 ClassA.apply(this,arguments);
59
60 this.name=sName;
61 this.sayName=function()
62 {
63 alert(this.name);
64 }
65 }
66 var oCarB=new ClassB("blue","myTest");
67 oCarB.showColor();
68 oCarB.sayName();
69 //-------------------原型链实现继承------------------------
70 function ClassA(){}
71 ClassA.prototype.color="red";
72 ClassA.prototype.showColor=function()
73 {
74 alert(this.color);
75 }
76 }
77 function ClassB(){}
78
79 ClassB.prototype=new ClassA();
80 ClassB.prototype.name="smatt";
81 ClassB.prototype.showName=function()
82 {
83 alert(this.name);
84 }
85 var oCar=new ClassB();
86 oCar.showColor();
87 oCar.showName();
88 //----------------------混合方式实现继承-----------------------------
89 function ClassA(sColor)
90 {
91 this.color=sColor;
92 }
93 ClassA.prototype.showColor=function()
94 {
95 alert(this.color);
96 }
97 function ClassB(sColor,sName)
98 {
99 ClassA.call(this,sColor);
100 this.name=sName;
101 }
102 ClassB.prototype=new ClassA();
103 ClassB.prototype.showName=function()
104 {
105 alert(this.name);
106 }
107 var oCar=new ClassB("red","smatt");
108 oCar.showColor();
109 oCar.showName();
110
111 </script>
1 <script type="text/javascript">
2 //------------------对象冒充实现继承-----------------
3 function ClassA(sColor)
4 {
5 this.color=sColor;
6 this.showColor=function()
7 {
8 alert(this.color);
9 }
10 }
11 function ClassC(iBig)
12 {
13 this.color=iBig;
14 this.showBig=function()
15 {
16 alert(this.color);
17 }
18
19 }
20 function ClassB(sColor,sName,iBig)
21 {
22 this.newMethed=ClassA;
23 this.newMethed(sColor);
24 delete this.newMethed;
25
26 this.newMethed=ClassC;
27 this.newMethed(iBig);
28 delete this.newMethed;
29
30 this.name=sName;
31 this.sayName=function()
32 {
33 alert(this.name);
34 }
35 }
36 //var oCarA=new ClassA("red");
37 //oCarA.showColor();
38 var oCarB=new ClassB("blue","myTest",100);
39 oCarB.showColor();
40 oCarB.showBig();
41 oCarB.sayName();
42 //----------------call或apply方法实现继承---------------------
43 function ClassA(sColor)
44 {
45 this.color=sColor;
46 this.showColor=function()
47 {
48 alert(this.color);
49 }
50 }
51 function ClassB(sColor,sName)
52 {
53 //this.newMethed=ClassA;
54 //this.newMethed(sColor);
55 //delete this.newMethed;
56 //ClassA.call(this,sColor);
57 //ClassA.apply(this,new Array(sColor));
58 ClassA.apply(this,arguments);
59
60 this.name=sName;
61 this.sayName=function()
62 {
63 alert(this.name);
64 }
65 }
66 var oCarB=new ClassB("blue","myTest");
67 oCarB.showColor();
68 oCarB.sayName();
69 //-------------------原型链实现继承------------------------
70 function ClassA(){}
71 ClassA.prototype.color="red";
72 ClassA.prototype.showColor=function()
73 {
74 alert(this.color);
75 }
76 }
77 function ClassB(){}
78
79 ClassB.prototype=new ClassA();
80 ClassB.prototype.name="smatt";
81 ClassB.prototype.showName=function()
82 {
83 alert(this.name);
84 }
85 var oCar=new ClassB();
86 oCar.showColor();
87 oCar.showName();
88 //----------------------混合方式实现继承-----------------------------
89 function ClassA(sColor)
90 {
91 this.color=sColor;
92 }
93 ClassA.prototype.showColor=function()
94 {
95 alert(this.color);
96 }
97 function ClassB(sColor,sName)
98 {
99 ClassA.call(this,sColor);
100 this.name=sName;
101 }
102 ClassB.prototype=new ClassA();
103 ClassB.prototype.showName=function()
104 {
105 alert(this.name);
106 }
107 var oCar=new ClassB("red","smatt");
108 oCar.showColor();
109 oCar.showName();
110
111 </script>