Skip to content

Commit 0c52f5e

Browse files
add: current context
1 parent 974ae30 commit 0c52f5e

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

Diff for: 28_object_oriented_programming/1_object.js

+1-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Object
22

3-
// What is Object?
3+
// What
44
// - It is collection of properties and method.
55

66
// Example
@@ -21,21 +21,3 @@ const object = {
2121
// - We need object to store data in proper way.
2222
// - It is like creating box/ container to store the specific item inside it. So it easy to access them.
2323

24-
// How object work?
25-
const user = {
26-
username: "test user",
27-
userId: "123",
28-
29-
getUserName: function () {
30-
console.log(username); // It doesn't know which user are you talking about.
31-
console.log(this.username); // By using this it will refer to current object.
32-
// user.getUserName === this.username
33-
},
34-
};
35-
36-
// Context/ Current Context
37-
// - When you are talking with your friend about some event that is host on MONDAY.
38-
// - But you need to also clearly mention that which MONDAY are you talking about it was last week, current week or upcoming week.
39-
// - This is the same for `this` keyword. It help to find the current context.
40-
// - Suppose you have two object and both of them have same method. If you call the method how it going to check that which object's method are you trying to access.
41-
// - By using `this` keyword you are telling that which MONDAY are you talking about. I mean which object you are trying to access.

Diff for: 28_object_oriented_programming/2_current_contex.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Context/ Current Context
2+
// - When you are talking with your friend about some event that is host on MONDAY.
3+
// - But you need to also clearly mention that which MONDAY are you talking about it was last week, current week or upcoming week.
4+
// - This is the same for `this` keyword. It help to find the current context.
5+
// - Suppose you have two object and both of them have same method. If you call the method how it will going to check that which object's method are you trying to access.
6+
// - By using `this` keyword you are telling that which MONDAY are you talking about. I mean which object you are trying to access.
7+
8+
const user = {
9+
username: "test user",
10+
userId: "123",
11+
12+
getUserName: function () {
13+
console.log(username); // It doesn't know which user are you talking about.
14+
console.log(this.username); // By using this it will refer to current object.
15+
// user.getUserName === this.username
16+
},
17+
};

Diff for: 28_object_oriented_programming/new_file.js renamed to 28_object_oriented_programming/4_prototype.js

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

33
// 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.
44

5+
// 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”:
6+
7+
// 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”.
8+
59
// What
610
// - Prototype is object.
711
// - Other objects inherit properties and methods from the prototype.
@@ -56,5 +60,5 @@ regularFunction.prototype.printMethod = function () {
5660
console.log(this.parameter);
5761
};
5862

59-
const object1 = regularFunction("argument");
60-
const object2 = regularFunction("argument");
63+
const object1 = new regularFunction("argument");
64+
const object2 = new regularFunction("argument");

Diff for: README.md

+6
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,9 @@
146146
2. Asynchronous programming
147147
1. setTimeout()
148148
2. setInterval()
149+
150+
151+
<!-- OOP -->
152+
1. Literal Object
153+
2. Constructor Function
154+
3.

0 commit comments

Comments
 (0)