-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
276 lines (218 loc) · 6.77 KB
/
index.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
console.log("Hello World from OOP-JS");
/**Object Literal */
console.log("***********OBJECT LITERAL SYNTAX *************");
const circle = {
radius: 1,
location: {
x: 1,
y: 1,
},
draw: function () {
console.log("draw");
},
};
circle.draw();
/**Factory Fuction */
console.log("***********FACTORY*************");
function createCircle(radius) {
return {
radius,
draw: function () {
console.log("draw");
},
};
}
const circledos = createCircle(1);
circledos.draw();
/**Constructor Fuction */
console.log("***********CONSTRUCTOR FUCTION*************");
function Circle(radius) {
this.radius = radius;
this.draw = function () {
console.log("draw a circle with radio", this.radius);
};
}
const newConstructorFunctionCircle = new Circle(1);
newConstructorFunctionCircle.draw();
console.log("***************CONSTRUCTORS*************");
new Object(); // Object Literal ==> x={}
new String(); // String Literal ==> ''. "", ``
new Boolean(); // Boolean Literal ==> true, false
new Number(); // Number Literal ==> 1,2,3, 4.5, -2
console.log("Functions are Objects");
new Function(); // Constructor Function
const Circle1 = new Function(
"radius",
`
this.radius = radius;
this.draw = function () {
console.log("draw a circle with radio", this.radius);
};
`
);
const circleObject = new Circle1(1);
console.log("Creating objects with new Function() Constructor");
console.log(circleObject);
console.log("Creating objects with method call, apply and bind");
Circle.call({}, 1); // Multiple Arguments ==> Circle.call({},1,2,3,4)/ this expression is the same like *** new Circle1(1);***
Circle.apply({}, [1, 2, 3]); // With apply you pass the Argumens in an Array [1,2,2,3,34,0]
console.log("***************VALUES PRIMITIVES AND REFERENCES*************");
console.log(`Values types `, ` Reference Types`);
console.log(` Numbers `, ` Object`);
console.log(` String `, ` Functions`);
console.log(` Boolean `, ` Array`);
console.log(` Symbol `);
console.log(` undefined `);
console.log(` null `);
console.log(`**********************************************`);
console.log(`Primitives are copied by their values and`);
console.log(`Object are copied by their reference`);
console.log(`**********************************************`);
console.log("Value type demostration");
let x = 10; // Value of X is assign 20;
console.log(`let x = 10;`);
let y = x; // value of Y is assign x that have the value 20 and it is copied to Y
console.log(`let y = x;`);
x = 20;
console.log(`x=20;`);
/** Those values x and y are independence of each others*/
console.log(`Those values x and y are independence of each others`);
console.log(`x===>`, x);
console.log(`y===>`, y);
console.log("Reference type demostration");
let x1 = { value: 10 }; // Value of X is assign 20;
console.log(`let x1 = { value: 10 };`);
let y1 = x1; // value of Y is assign x that have the value 20 and it is copied to Y
console.log(`let y1 = x1;`);
x1.value = 20;
console.log(`x1.value = 20;`);
console.log(`x1 ===>`, x1);
console.log(`y1 ===>`, y1);
/** When you use an object that object is not stored in this variable */
console.log(`When you use an object that object is not stored in this`);
console.log(`variable That object is stored somewhere else in the`);
console.log(`memory and the address of that memory location is`);
console.log(`stored inside that memory variable it is the`);
console.log(`address or the reference that is copied, because`);
console.log(`x1 and y1 are pointing to the same object in memmory`);
console.log(`Example of function pass value types`);
let number = 10;
function increase(number) {
number++;
}
increase(number);
console.log(`
let number = 10;
function increase(number) {
number++;
}
increase(number);
`);
console.log(number);
console.log(`Example of function pass reference types`);
let numberObject = { value: 10 };
function increaseObject(numberObject) {
numberObject.value++;
}
increaseObject(numberObject);
console.log(`
let numberObject = {value:10};
function increaseObject(numberObject) {
numberObject.value++;
}
increaseObject(numberObject);
`);
console.log(numberObject);
console.log(`***********Adding or Removing Properties*************`);
console.log(`***********Adding Properties*************`);
/**First Approach */
console.log("First Approach Dot Notation");
circle.location = {
x: 1,
};
console.log(`
circle.location = {
x: 1,
};
`);
/**Second Approach */
console.log("First Approach Bracket Notation");
console.log(`
circle["location2"] = { x: 3 };
`);
circle["location2"] = { x: 3 };
console.log(circle);
console.log(`***********Remove Properties*************`);
console.log(`delete circle.location with the dot notation`);
console.log(`or use the brackets notation delete circle{'location'};`);
delete circle.location;
console.log(circle);
/**Abstraction
*
*/
console.log(`******ABSTRACTION******`);
console.log("Declaration of Private Properties and Methods");
console.log("With ***LET*** to declare like a local variable and convert");
console.log("the functions an private methods");
function CircleAbstraction(radius) {
this.radius = radius;
let defaultLocation = { x: 0, y: 0 };
let computedOptimumLocation = function () {};
console.log(`
this.draw = function () {
computedOptimumLocation();/**This is access for closure */
};`);
this.draw = function () {
computedOptimumLocation(); /**This is access for closure */
//You can access from here
//defaultLocation
//this.radius
};
}
const circleObjectAbs = new CircleAbstraction(10);
console.log(
`Now we only have access to radios property and the draw() function`
);
function CircleGettesSetters(radius) {
this.radius = radius;
let defaultLocation = { x: 0, y: 0 };
this.getDefaultLocation = function () {
return defaultLocation;
};
this.draw = function () {
console.log("Draw");
};
Object.defineProperty(this, "defaultLocation", {
get: function () {
return defaultLocation;
},
set: function (value) {
if (!value.x || !value.y) throw new Error("Invalid location");
defaultLocation = value;
},
});
}
const circleObjectGetterSetter = new CircleGettesSetters(10);
//circleObjectGetterSetter.defaultLocation = 1;
console.log(circleObjectGetterSetter);
console.log(`******INHERITANCE******`);
console.log(`***We have 2 types of Inheritance Classical and Prototypical***`);
console.log(`******INHERITANCE IN ES6 WITH CLASSES******`);
class ShapeC {
constructor(color) {
this.color = color;
}
move() {
console.log("move");
}
}
class CircleC extends ShapeC {
constructor(color, radius) {
super(color);
this.radius = radius;
}
draw() {
console.log("draw");
}
}
const c = new CircleC("red", 5);