-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectsOrientedProgramming.js
229 lines (189 loc) · 5.58 KB
/
ObjectsOrientedProgramming.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
//// part of Free Code Camp JS algoritm & basic structures
// adds all of the own properties to the array ownProps:
let ownProps = [];
for (let property in duck) {
if(duck.hasOwnProperty(property)) {
ownProps.push(property);
}
}
console.log(ownProps); // prints [ "name", "numLegs" ]
/////////////////////////////////////////////////////////////////////
/// object own oroperty and object.prototip property
function Dog(name) {
this.name = name;
}
Dog.prototype.numLegs = 4; // ad prototip property to Dog's object
let beagle = new Dog("Snoopy"); // new Dog's object. Its be have dog's prototip property, but itisn own property!!
let ownProps = [];
let prototypeProps = [];
for (let property in beagle) {
if (beagle.hasOwnProperty(property)) {
ownProps.push(property);
} else {
prototypeProps.push(property);
}
}
///////////////////////////////////////
/// check constructor property
function Dog () {
this.name;
this.color;
}
let Burt = new Dog();
console.log(Burt instanceof Dog);
////////////////////////
// and another metod with property "constructor"
function Dog(name) {
this.name = name;
}
function joinDogFraternity(candidate) {
return candidate.constructor == Dog ? true : false;
}
//////////////////////////////////////////
/// Remember to Set the Constructor Property when Changing the Prototype!!!!
///////////////////////////////////////// Dog.prototipe = { constructor: Dog,.........}
//check prototipe -> born object consrtuctor ""?
function Dog(name) {
this.name = name;
}
let beagle = new Dog("Snoopy");
// Add your code below this line
Dog.prototype.isPrototypeOf(beagle); // return true
///
///Understand the Prototype Chain
////////////////////////////////////////////////////////////////INHERITED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// //////////////////////////////////////////// Create superType for prototype
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");
}
};
let myau = Object.create(Animal.prototype);
let uHr = Object.create(Animal.prototype);
let ups = Object.create(Animal.prototype);
myau.eat();
uHr.eat();
ups.eat();
////////////////////Set the Child's Prototype to inherited of the Parent
//
/// ChildObject.prototype = Object.create (ParentObject.prototype);
function Animal() { }
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
function Dog() { }
Dog.prototype = Object.create(Animal.prototype); // create instance from Animal supertype for Dog.proto!!
let beagle = new Dog();
beagle.eat(); // Should print "nom nom nom"
////////////////////////////////////////
//
// add the function to child prototyp
function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };
function Dog() { }
// Add your code below this line
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function () { // here is. add the unicum function to Dog
console.log("Woof!");
}
// Add your code above this line
let beagle = new Dog();
beagle.eat(); // Should print "nom nom nom"
beagle.bark(); // Should print "Woof!"
//////////// 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 MIXIN !!!
let flyMixin = function(object) {
object.fly = function () {
return "I can fly!";
}
}
let bird = {
name: "Donald",
numLegs: 2
};
let plane = {
model: "777",
numPassengers: 524
};
flyMixin(bird);
flyMixin(plane);
////////////Use Closure to Protect Properties Within an Object from Being Modified Externally
//
function Bird() {
let weight = 15; // only LOCAL!!!
this.getWeight = function() { // for access from anothet to get weight!
return weight;
}
}
let ducky = new Bird();
ducky.getWeight(); // returns 15
////////////////// Understand the Immediately Invoked Function Expression (IIFE)
////////////////////
(function () {
console.log ("Чирп, щебет!");
}) ();
//////////////////// Use an IIFE to Create a Module
//
let motionModule = (function () {
return {
glideMixin: function (obj) {
obj.glide = function() {
console.log("Gliding on the water");
};
},
flyMixin: function(obj) {
obj.fly = function() {
console.log("Flying, wooosh!");
};
}
}
}) (); // The two parentheses cause the function to be immediately invoked
motionModule.glideMixin(duck);
duck.glide();
///////////////////
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");
}
}
}
})();
/////////////////
/////////////////////
//its very cute for consentration one type metods to one place