-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtut3.html
More file actions
149 lines (120 loc) · 4.23 KB
/
tut3.html
File metadata and controls
149 lines (120 loc) · 4.23 KB
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
<html>
<head>
<title>Tut 3 Derek</title>
</head>
<body>
<h3> Tut 3 JS - OOP </h3>
<div>
<script language="javascript" type="text/javascript">
//constructor
function Animal() {
this.name = "No Name";
this.sound = "Grr";
this.owner = "No Owner";
}
Animal.prototype.setOwner = function (newOwner) {
if (typeof newOwner != 'undefined') {
this.owner = newOwner;
}
else {
document.write("Please eneter a valid owner <br/>");
}
}
Animal.prototype.getOwner = function () {
return this.owner;
}
Animal.prototype.setName = function(newName)
{
if(typeof newName != 'undefined')
{
this.name = newName;
}
else
{
document.write("Please enter a valid name <br/>");
}
}
Animal.prototype.getName = function()
{
return this.name;
}
Animal.prototype.setSound = function(newSound)
{
if( typeof newSound != 'undefined' )
{
this.sound = newSound;
}
else
{
document.write("Please eneter a valid sound <br/>");
}
}
Animal.prototype.getSound = function()
{
return this.sound;
}
var dog = new Animal();
dog.setName("Spot");
dog.setOwner("Paul");
dog.setSound("Woof");
document.write(dog.getName() + "<br/>" );
document.write(dog.getOwner() + "<br/>" );
document.write(dog.getSound() + "<br/>" );
document.write("<br/> ------------ <br/>");
function Cat()
{
Animal.call(this); // inherit everything from Animal
this.mode = "Happy";
}
//setting the Animal as superclass of the Cat
Cat.prototype = new Animal(); // if we are not doing this Cat
//will be consideredto be a subclass of Animal
Cat.prototype.constructor = Cat;
Cat.prototype.getMode = function()
{
return this.mode;
}
Cat.prototype.setMode = function(newMode)
{
if(typeof newMode != 'undefined')
{
this.mode = newMode;
}
else
{
document.write("Please enter a valid mode");
}
}
var sophie = new Cat();
sophie.setName("Sophie");
sophie.setOwner("Gabi");
sophie.setSound("Meow");
sophie.setMode("Cat");
document.write(sophie.getName() + " is " + sophie.getMode() + "<br/>");
//document.write("Huehue");
document.write("<p>");
document.write(sophie instanceof Cat);
document.write("</br>");
document.write(typeof(sophie) + " & " + typeof(Cat));
//method overloading
Cat.prototype.setStuff = function(newName, newSound, newOwner)
{
if((typeof newName != 'undifined')&&(typeof newName != 'undifined') && (typeof newName != 'undifined') )
{
// we are overwriting this method
Cat.prototype.setStuff = function(newName, newSound, newOwner)
{
this.name=newName;
this.sound=newSound;
this.owner=newOwner;
} // or whatever condition here
}else if ((typeof newName != 'undifined')||(typeof newName != 'undifined'))
{
document.write("We are going on this brench");
}
}
//polymorphism
</script>
</div>
</body>
</html>