forked from chuanxshi/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inheritence.html
177 lines (155 loc) · 6 KB
/
inheritence.html
1
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!--
* One base class is created.
* Another child class created extending the base class to demonstrate the inheritance.
* One subclass created extending the second class to demonstrate the multilevel inheritance.
* All the classes are having static, non-static, private and public members.
* In the test client we are demonstrating some of these features.
* To test the other features, additional caller codes has to be added.
-->
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns - Inheritance</title>
<meta charset="utf-8">
</head>
<script>
//Defination of top superclass
function topSuperClass() {
}
//static members
topSuperClass.staticProperty = function() {
//static private members
var TSCstaticPrivateVariable = "I am static private variable of top super class";
var TSCstaticPrivateMethod = function() {
return "I am a static private method of top super class";
};
return {
//static public members
TSCstaticPublicVariable : "I am static public variable of top super class",
TSCstaticPublicMethod : function() {
return "I am a static public method of top super class";
}
};
}();
//non static members
topSuperClass.prototype.TSCnonStaticProperty = function() {
//non static private members
var TSCnonStaticPrivateVariable = "I am non static private variable of top super class";
var TSCnonStaticPrivateMethod = function() {
return "I am a non static private method of top super class";
};
return {
//non static public members
TSCnonStaticPublicVariable : "I am non static public variable of top super class",
TSCnonStaticPublicMethod : function() {
alert(TSCnonStaticPrivateMethod());// accessing the private member inside class
return "I am a non static public method of top super class";
}
};
}();
//Defination of superclass
function superClass() {
}
//inheritance declaration
superClass.prototype = new topSuperClass();
//static members
superClass.staticProperty = function() {
//static private members
var SCstaticPrivateVariable = "I am static private variable of super class";
var SCstaticPrivateMethod = function() {
return "I am a static private method of super class";
};
return {
//static public members
SCstaticPublicVariable : "I am static public variable of super class",
SCstaticPublicMethod : function() {
return "I am a static public method of super class";
}
};
}();
//non static members
superClass.prototype.SCnonStaticProperty = function() {
//non static private members
var SCnonStaticPrivateVariable = "I am non static private variable of super class";
var SCnonStaticPrivateMethod = function() {
return "I am a non static private method of super class";
};
return {
//non static public members
SCnonStaticPublicVariable : "I am non static public variable of super class",
SCnonStaticPublicMethod : function() {
return "I am a non static public method of super class";
}
};
}();
//Defination of subclass
function subClass() {
}
//inheritance Declaration
//subClass.prototype = new topSuperClass();
subClass.prototype = new superClass();
//static members
subClass.staticProperty = function() {
//static private members
var SBCstaticPrivateVariable = "I am static private variable of sub class";
var SBCstaticPrivateMethod = function() {
return "I am a static private method of sub class";
};
return {
//static public members
SBCstaticPublicVariable : "I am static public variable of sub class",
SBCstaticPublicMethod : function() {
return "I am a static public method of sub class";
}
};
}();
//non static members
subClass.prototype.SBCnonStaticProperty = function() {
//non static private members
var SBCnonStaticPrivateVariable = "I am non static private variable of sub class";
var SBCnonStaticprivateMethod = function() {
return "I am a non static private method of sub class";
};
return {
//non static public members
SBCnonStaticPublicVariable : "I am non static public variable of sub class",
SBCnonStaticPublicMethod : function() {
return "I am a non static public method of sub class";
}
};
}();
//Inheritence Defination
//superClass.prototype = new topSuperClass();
//subClass.prototype = new superClass();
var test = function() {
//Demonstration of base class member access
var topSuperClassObj = new topSuperClass();// Creation of object of the base class
//Calling the private static members with class refferene
//alert(topSuperClass.staticProperty.TSCstaticPrivateVariable);//Not visible outside class
//alert(topSuperClass.staticProperty.TSCstaticPrivateMethod());//Not visible outside class
//Accessing the public static members
//alert(topSuperClass.staticProperty.TSCstaticPublicVariable);//Visible as public member variable
//alert(topSuperClass.staticProperty.TSCstaticPublicMethod());//Visible as public method
//Calling the private non static mebbers with object refferene
//alert(topSuperClassObj.TSCnonStaticProperty.TSCnonStaticPrivateVariable);//Not visible outside class
//alert(topSuperClassObj.TSCnonStaticProperty.TSCnonStaticPrivateMethod());//Not visible outside class
//Calling the public non static mebbers with object refferene
//alert(topSuperClassObj.TSCnonStaticProperty.TSCnonStaticPublicVariable);//visible outside class
alert(topSuperClassObj.TSCnonStaticProperty.TSCnonStaticPublicMethod());//visible outside class
//How to access super class methods using subclass reference
//var subClassObj = new subClass();
//alert(subClassObj.SBCnonStaticProperty.SBCnonStaticPublicMethod());
//alert(subClass.staticProperty.SBCstaticPublicMethod());
//alert(subClassObj.SCnonStaticProperty.SCnonStaticPublicMethod());
//alert(superClass.staticProperty.SCstaticPublicMethod());
//alert(subClassObj.TSCnonStaticProperty.TSCnonStaticPublicMethod());
//alert(topSuperClass.staticProperty.TSCstaticPublicMethod());
}
</script>
<body>
<h1 align="center">JAVASCRIP INHERITANCE</h1>
<p align="center">
<input type="button" value="Test" onclick="test()">
</p>
</body>
</html>