Skip to content

Commit 89678d5

Browse files
refactor: oops
1 parent 0c52f5e commit 89678d5

File tree

6 files changed

+34
-30
lines changed

6 files changed

+34
-30
lines changed

28_object_oriented_programming/1_object.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
// What
44
// - It is collection of properties and method.
55

6+
// Why
7+
// - We need object to store data in proper way.
8+
// - It is like creating box/ container to store the specific item inside it. So it easy to access them.
9+
610
// Example
711
// - In JavaScript there is Object Literal {}.
812
// - This means we are declaring literally an object.
@@ -16,8 +20,3 @@ const object = {
1620
console.log(parameter);
1721
},
1822
};
19-
20-
// Why we need object?
21-
// - We need object to store data in proper way.
22-
// - It is like creating box/ container to store the specific item inside it. So it easy to access them.
23-

28_object_oriented_programming/2_current_contex.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// Context/ Current Context
2+
3+
// What
24
// - When you are talking with your friend about some event that is host on MONDAY.
35
// - But you need to also clearly mention that which MONDAY are you talking about it was last week, current week or upcoming week.
46
// - This is the same for `this` keyword. It help to find the current context.

28_object_oriented_programming/2_constructor_function.js 28_object_oriented_programming/3_constructor_function.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Constructor Function
22

3-
// What is Construction Function?
3+
// Backstory
4+
// - In other programming language like C++ and Java.
5+
// - We have to create a class then inside the class we need to declare constructor after that we can create an object (Instance/ Instance Object).
6+
7+
// What
48
// - Yes, in JavaScript, you can create constructor functions without using the class keyword or the constructor keyword. Instead, you can define a regular function and manually assign properties and methods to the newly created object using the this keyword.
59

610
function ConstructorFunction(parameter1, parameter2) {
@@ -17,7 +21,5 @@ function ConstructorFunction(parameter1, parameter2) {
1721
}
1822

1923
// Creating new instance
20-
const object = new ConstructorFunction("value1", "value2");
21-
// console.log(object);
22-
23-
24+
const instanceObject = new ConstructorFunction("argument1", "argument2");
25+
console.log(instanceObject);

28_object_oriented_programming/3_new_keyword.js 28_object_oriented_programming/4_new_keyword.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// `new` Keyword
22

3-
// What is `new` Keyword?
3+
// Object === Instance === Instance Object
4+
5+
// What
46
// - In JavaScript, the new keyword is used to create an instance of an object or to invoke a constructor function.
57

6-
// Why we need `new` keyword?
8+
// Why
79
// - If you create a new instance without the `new` keyword it will update the value of previously created instance.
810

911
// How the `new` keyword work?
@@ -14,7 +16,7 @@
1416
// 2. Binds the newly created object as the `this` keyword within the constructor function.
1517
// - Sets the prototype of the newly created object to the prototype property of the constructor function.
1618
// - Executes the constructor function, which initializes the newly created object with properties and methods.
17-
// - Returns the newly created object.
19+
// - Returns the newly created object.
1820

1921
function User(username, userID, userEmail) {
2022
this.username = username;
@@ -27,6 +29,7 @@ function User(username, userID, userEmail) {
2729
return this;
2830
}
2931

32+
// Try this without `new` keyword
3033
const userOne = new User("user1", "123", "[email protected]");
3134
const userTwo = new User("user2", "456", "[email protected]");
3235

Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
// Prototype (Better to call Prototype Object)
22

3-
// Inheritance: In JavaScript is a mechanism that allows one object to acquire properties and methods of another object. This is primarily achieved through the prototype chain.
3+
// What
4+
5+
// Inheritance: In JavaScript is a mechanism that allows one object to acquire properties and methods of another object.
46

57
// Prototype: In JavaScript, objects have a special hidden property [[Prototype]] (as named in the specification), that is either null or references another object. That object is called “a prototype”:
68

79
// Prototype Inheritance: When we read a property from object, and it’s missing, JavaScript automatically takes it from the prototype. In programming, this is called “prototypal inheritance”.
810

9-
// What
10-
// - Prototype is object.
11-
// - Other objects inherit properties and methods from the prototype.
12-
// - Every object in JavaScript has a prototype.
13-
1411
// Why
1512
// - The prototype system allows JavaScript to implement inheritance and to share properties and methods across instances.
1613
// - This makes code reuse and object - oriented programming possible in JavaScript.
@@ -22,22 +19,22 @@
2219
// - Yes, function is function.
2320
// - In JavaScript everything is object.
2421

25-
function regularFunction(parameter) {
22+
function ConstructorFunction(parameter) {
2623
console.log(parameter);
2724
}
2825

2926
// Normally calling function
30-
console.log(regularFunction("argument"));
27+
console.log(ConstructorFunction("argument"));
3128

3229
// Adding property into function like an object
33-
regularFunction.power = 2;
34-
console.log(regularFunction.power);
30+
ConstructorFunction.power = 2;
31+
console.log(ConstructorFunction.power);
3532

3633
// Prototype in function
37-
console.log(regularFunction.prototype);
34+
console.log(ConstructorFunction.prototype);
3835

3936
// Note
40-
// - Every object in JavaScript inherit properties and method of parent object. It continues.
37+
// - Every object in JavaScript inherit properties and method of parent object. It's continues process.
4138
// Object <- (Prototype + Inheriting) <- Parent Object <- (Prototype + Inheriting) <- Grand Parent Object
4239

4340
// Array <- (Prototype + Inheriting) <- Object <- .length
@@ -48,17 +45,18 @@ console.log(regularFunction.prototype);
4845

4946
// - If you wondering what will you get at the end of this prototype inheritance chain is `null`.
5047

51-
function regularFunction(parameter) {
48+
// Constructor Function + Prototype + `new` keyword
49+
function ConstructorFunction(parameter) {
5250
this.parameter = parameter;
5351
}
5452

55-
regularFunction.prototype.method = function () {
53+
ConstructorFunction.prototype.method = function () {
5654
this.parameter++;
5755
};
5856

59-
regularFunction.prototype.printMethod = function () {
57+
ConstructorFunction.prototype.printMethod = function () {
6058
console.log(this.parameter);
6159
};
6260

63-
const object1 = new regularFunction("argument");
64-
const object2 = new regularFunction("argument");
61+
const instanceObject1 = new ConstructorFunction("argument");
62+
const instanceObject2 = new ConstructorFunction("argument");
File renamed without changes.

0 commit comments

Comments
 (0)