Skip to content

Commit 6cc1f4b

Browse files
refactor: class
1 parent 89678d5 commit 6cc1f4b

17 files changed

+264
-80
lines changed

28_object_oriented_programming/1_object.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Object
22

3+
// Object-oriented programming (OOP): It is a programming paradigm that organizes code into objects, which are instances of classes. It provides a way of structuring programs by representing real-world entities or concepts as objects that have data (attributes or properties) and behaviors (methods or functions).
4+
35
// What
46
// - It is collection of properties and method.
57

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Adding the custom method to the JavaScript Origin Object.
2+
Object.prototype.customMethod = function () {
3+
console.log("I am the origin object's custom method.");
4+
};
5+
6+
const childObject = {
7+
key: "value",
8+
9+
method: function () {
10+
console.log("I am the child object's method");
11+
},
12+
};
13+
14+
// Accessing method from child object
15+
console.log(childObject.method());
16+
17+
// Accessing custom method from origin object because of prototype
18+
console.log(childObject.customMethod());
19+
20+
// Because of the custom method is in origin object. You can access it form where ever
21+
const array = [1, 2, 3, 4, 5];
22+
console.log(array.customMethod());
23+
24+
// __proto__
25+
26+
// __proto__ is property in object to set other object as a property.
27+
// Prototype is name for theory discussion.
28+
// __proto__ is name for actual code.
29+
30+
const grandFather = {
31+
grandFatherCar: "Doge Charger 1960",
32+
};
33+
34+
const father = {
35+
fatherCar: "BMW M3 GTR",
36+
// __proto__: grandFather,
37+
};
38+
39+
const child = {
40+
childCar: "Hot wheels",
41+
// __proto__: father,
42+
};
43+
44+
// console.log(child.grandFatherCar);
45+
// console.log(child.fatherCar);
46+
// console.log(child.childCar);
47+
48+
// Modern Syntax
49+
50+
// Setting up the prototype chain using Object.setPrototypeOf
51+
Object.setPrototypeOf(father, grandFather);
52+
Object.setPrototypeOf(child, father);
53+
54+
console.log(child.grandFatherCar);
55+
console.log(child.fatherCar);
56+
console.log(child.childCar);

29_call/1_call.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// .call()
2+
3+
// What
4+
// - The .call() method in JavaScript is a special method available on all functions. It allows you to call a function and specify what this should refer to inside that function.
5+
6+
// Why
7+
// - In JavaScript, the value of this inside a function depends on how the function is called.
8+
// - Sometimes, you might want to call a function in a way that this refers to a specific object
9+
// - The.call() method gives you that control.
10+
11+
// Example - 1
12+
function setUsername(username) {
13+
this.username = username;
14+
}
15+
16+
function CreateUser(username, email, password) {
17+
// Using .call() to set the context of this to the current instance
18+
setUsername.call(this, username);
19+
20+
this.email = email;
21+
this.password = password;
22+
}
23+
24+
const userOne = new CreateUser("testUser", "[email protected]", "123");
25+
console.log(userOne);
26+
27+
// Example - 2
28+
const person = {
29+
firstName: "John",
30+
lastName: "Doe",
31+
fullName: function () {
32+
return this.firstName + " " + this.lastName;
33+
},
34+
};
35+
36+
const anotherPerson = {
37+
firstName: "Jane",
38+
lastName: "Smith",
39+
};
40+
41+
// Call the fullName method of person, but set `this` to anotherPerson
42+
console.log(person.fullName.call(anotherPerson));
File renamed without changes.

30_class/1_class.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Class
2+
3+
// What
4+
// - A class in JavaScript is a blueprint for creating objects.
5+
// - It encapsulates data and behavior(methods) that objects created from the class will share.
6+
// - Classes were introduced in ECMAScript 6(ES6) to provide a cleaner and more intuitive way to create and manage objects and their relationships.
7+
8+
// Why
9+
// - Organization: They help organize code by grouping related data and behavior.
10+
// - Reusability: Code can be reused more effectively by creating multiple objects from the same class.
11+
// - Inheritance: They allow for inheritance, where one class can extend another and inherit its properties and methods.
12+
// - Encapsulation: They help encapsulate and hide the complexity of data and behavior within a class.
13+
14+
// Syntax
15+
// 1. Creating class using `class` keyword
16+
class MyClass {
17+
constructor(parameter1, parameter2) {
18+
this.parameter1 = parameter1;
19+
this.parameter2 = parameter2;
20+
}
21+
22+
method() {
23+
console.log(`We are ${this.parameter1} & ${this.parameter2}`);
24+
console.log("I am method");
25+
}
26+
}
27+
28+
const instanceObject = new MyClass("argument1", "argument2");
29+
console.log(instanceObject.method());
30+
31+
// 2. Creating class with class expression
32+
const variable = class {
33+
constructor() {}
34+
};
35+
36+
// Example
37+
class User {
38+
constructor(username, email, password) {
39+
this.username = username;
40+
this.email = email;
41+
this.password = password;
42+
}
43+
44+
encryptPassword() {
45+
return `${this.password}abc`;
46+
}
47+
48+
changeUsername() {
49+
return `${this.username.toUpperCase()}`;
50+
}
51+
}
52+
53+
const userOne = new User("userOne", "[email protected]", "123");
54+
console.log(userOne.encryptPassword());
55+
console.log(userOne.changeUsername());
56+
57+
// Behind the Scene
58+
function CreateUser(username, email, password) {
59+
this.username = username;
60+
this.email = email;
61+
this.password = password;
62+
}
63+
64+
CreateUser.prototype.encryptPassword = function () {
65+
return `${this.password}abc`;
66+
};
67+
CreateUser.prototype.changeUsername = function () {
68+
return `${this.username.toUpperCase()}`;
69+
};
70+
71+
const user = new CreateUser("user", "[email protected]", "123");
72+
console.log(user.encryptPassword());
73+
console.log(user.changeUsername());
74+
75+
// Note
76+
// - Class in JavaScript doesn't work like other programming language. It's just a syntactic sugar which look like we are using class but in the background it is using Constructor Function and Prototype Inheritance.
77+
// - Always add a method named constructor();
78+
// - Class can't be hoisted.
79+
// - Class are first class citizen just like first class function.

30_class/2_inheritance.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Inheritance
2+
3+
// Example
4+
class User {
5+
constructor(username) {
6+
this.username = username;
7+
}
8+
9+
printUsername() {
10+
console.log(`Username is ${this.username}`);
11+
}
12+
}
13+
14+
// Backstory
15+
// - Do you remember we are using the __proto__ (Prototype) to connect two object to share parent object properties and method for child object.
16+
// - We are doing same thing here. Just form of class and extends keywords. It's syntactic sugar.
17+
// - Like how we create object using the object literal, here we are using class to create object (Fancy name is "instance").
18+
// - Now extending so called class to another class via `extends` keyword === __proto__
19+
20+
class Teacher extends User {
21+
constructor(username, email, password) {
22+
// - The `super` keyword in JavaScript is crucial for working with class inheritance. It allows subclasses to initialize and utilize parent class properties and methods,
23+
super(username);
24+
25+
this.email = email;
26+
this.password = password;
27+
}
28+
29+
addCourse() {
30+
return console.log(`Course Instructor: ${this.username}`);
31+
}
32+
}
33+
34+
const sam = new Teacher("samwhite", "[email protected]", "123");
35+
sam.addCourse();
36+
37+
console.log(sam === Teacher);
38+
console.log(sam === User);
39+
40+
console.log(sam instanceof Teacher);
41+
console.log(sam instanceof User);

30_class/3_static.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Static
2+
3+
// What
4+
// - Static is a keyword in JavaScript used to define static methods and properties for a class.
5+
// - Static properties cannot be directly accessed on instances of the class. Instead, they're accessed on the class itself.
6+
7+
// Why
8+
// - Static methods and properties are used when you want to create functions or data that are not tied to any specific instance of a class.
9+
10+
// How
11+
// - You can define a static method or property by using the static keyword inside a class definition.
12+
13+
// Syntax
14+
class ClassName {
15+
static staticPropertyName = "value";
16+
static staticMethodName() {
17+
console.log(`I am static method`);
18+
}
19+
}
20+
21+
console.log(ClassName.staticMethodName());
22+
23+
// Example
24+
class User {
25+
static company = "ABC Company";
26+
27+
constructor(username, email, password) {
28+
this.username = username;
29+
this.email = email;
30+
this.password = username;
31+
}
32+
33+
static createId() {
34+
return `123`;
35+
}
36+
37+
getUsername() {
38+
console.log(`Username: ${this.username}`);
39+
}
40+
}
41+
42+
const userOne = new User("userOne");
43+
// console.log(userOne.createId());
44+
console.log(User.createId());

pending_concept/1_class/0_what_is_oop.js

-6
This file was deleted.

pending_concept/1_class/1_class.js

-32
This file was deleted.

pending_concept/1_class/2_class_method.js

-12
This file was deleted.
File renamed without changes.
File renamed without changes.

pending_concept/CallApplyBind/01Call.js

-30
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)