-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7) Object Oriented Programming.js
277 lines (252 loc) · 6.15 KB
/
7) Object Oriented Programming.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Create a Basic JavaScript Object
let dog = {
name: "Spot",
numLegs: 4
}
// Use Dot Notation to Access the Properties of an Object
let dog = {
name: "Spot",
numLegs: 4
};
console.log(dog.name, dog.numLegs);
// Create a Method on an Object
let dog = {
name: "Spot",
numLegs: 4,
sayLegs: function() {return "This dog has " + dog.numLegs + " legs.";}
};
console.log(dog.sayLegs());
// Make Code More Reusable with the this Keyword
let dog = {
name: "Spot",
numLegs: 4,
sayLegs: function() {return "This dog has " + this.numLegs + " legs.";}
};
console.log(dog.sayLegs());
// Define a Constructor Function
function Dog() {
this.name = "Spot";
this.color = "brown";
this.numLegs = 4;
}
// Use a Constructor to Create Objects
function Dog() {
this.name = "Rupert";
this.color = "brown";
this.numLegs = 4;
}
let hound = new Dog();
// Extend Constructors to Receive Arguments
function Dog(name, color) {
this.name = name;
this.color = color;
this.numLegs = 4;
}
let terrier = new Dog("Rupert", "brown");
// Verify an Object's Constructor with instanceof
function House(numBedrooms) {
this.numBedrooms = numBedrooms;
}
let myHouse = new House(4);
console.log(myHouse instanceof House);
// Understand Own Properties
function Bird(name) {
this.name = name;
this.numLegs = 2;
}
let canary = new Bird("Tweety");
let ownProps = [];
for (let property in canary) {
if(canary.hasOwnProperty(property)) {
ownProps.push(property);
}
}
console.log(ownProps);
// Use Prototype Properties to Reduce Duplicate Code
function Dog(name) {
this.name = name;
}
Dog.prototype.numLegs = 4;
let beagle = new Dog("Snoopy");
console.log(beagle.numLegs);
// Iterate Over All Properties
function Dog(name) {
this.name = name;
}
Dog.prototype.numLegs = 4;
let beagle = new Dog("Snoopy");
let ownProps = [];
let prototypeProps = [];
for (let property in beagle) {
if(beagle.hasOwnProperty(property)) {
ownProps.push(property);
} else {
prototypeProps.push(property);
}
}
console.log(ownProps);
console.log(prototypeProps);
// Understand the Constructor Property
function Dog(name) {
this.name = name;
}
function joinDogFraternity(candidate) {
if(candidate.constructor === Dog) {
return true;
} else {
return false;
}
}
// Change the Prototype to a New Object
function Dog(name) {
this.name = name;
}
Dog.prototype = {
numLegs: 4,
eat: function() {
console.log("nom nom nom");
},
describe: function() {
console.log("My name is " + this.name);
}
};
// Remember to Set the Constructor Property when Changing the Prototype
function Dog(name) {
this.name = name;
}
Dog.prototype = {
constructor: Dog,
numLegs: 4,
eat: function() {
console.log("nom nom nom");
},
describe: function() {
console.log("My name is " + this.name);
}
};
// Understand Where an Object’s Prototype Comes From
function Dog(name) {
this.name = name;
}
let beagle = new Dog("Snoopy");
console.log(Dog.prototype.isPrototypeOf(beagle));
// Understand the Prototype Chain
function Dog(name) {
this.name = name;
}
let beagle = new Dog("Snoopy");
Dog.prototype.isPrototypeOf(beagle);
Object.prototype.isPrototypeOf(Dog.prototype);
// Use Inheritance So You Don't Repeat Yourself
function Cat(name) {
this.name = name;
}
Cat.prototype = {
constructor: Cat,
};
function Bear(name) {
this.name = name;
}
Bear.prototype = {
constructor: Bear,
};
function Animal() { }
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
// Inherit Behaviors from a Supertype
function Animal() { }
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
let duck = Object.create(Animal.prototype);
let beagle = Object.create(Animal.prototype);
// Set the Child's Prototype to an Instance of the Parent
function Animal() { }
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
function Dog() { }
Dog.prototype = Object.create(Animal.prototype);
let beagle = new Dog();
// Reset an Inherited Constructor Property
function Animal() { }
function Bird() { }
function Dog() { }
Bird.prototype = Object.create(Animal.prototype);
Dog.prototype = Object.create(Animal.prototype);
Bird.prototype.constructor = Bird;
Dog.prototype.constructor = Dog;
let duck = new Bird();
let beagle = new Dog();
// Add Methods After Inheritance
function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };
function Dog() { }
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function() { console.log("Woof!"); };
let beagle = new Dog();
// Override Inherited Methods
function Bird() { }
Bird.prototype.fly = function() { return "I am flying!"; };
function Penguin() { }
Penguin.prototype = Object.create(Bird.prototype);
Penguin.prototype.constructor = Penguin;
Penguin.prototype.fly = function() { return "Alas, this is a flightless bird."; };
let penguin = new Penguin();
console.log(penguin.fly());
// Use a Mixin to Add Common Behavior Between Unrelated Objects
let bird = {
name: "Donald",
numLegs: 2
};
let boat = {
name: "Warrior",
type: "race-boat"
};
let glideMixin = function(obj) {
obj.glide = function() {
console.log("Gliding, wooosh!");
}
};
glideMixin(bird);
glideMixin(boat);
bird.glide();
boat.glide();
// Use Closure to Protect Properties Within an Object from Being Modified Externally
function Bird() {
let weight = 15;
this.getWeight = function() {
return weight;
}
}
// Understand the Immediately Invoked Function Expression (IIFE)
(function() {
console.log("A cozy nest is ready");
})();
makeNest();
// Use an IIFE to Create a Module
let funModule = (function() {
return {
isCuteMixin: function(obj) {
obj.isCute = function() {
return true;
};
},
singMixin: function(obj) {
obj.sing = function() {
console.log("Singing to an awesome tune");
};
}
}
})();