Skip to content

Commit 47a0d68

Browse files
committed
docs: constructors & prototypes
1 parent 2301848 commit 47a0d68

4 files changed

+166
-9
lines changed

README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ This advanced JavaScript course is designed for experienced developers looking t
1414
### [Module 1: Advanced JavaScript Concepts](./module-1-advanced-javascript-concepts.md)
1515

1616
- 1.1. [Closures and Scopes](./module-1.1-closures-and-scopes.md)
17-
- 1.2. [Promises and Async/Await](#promises-and-asyncawait)
18-
- 1.3. [Functional Programming](#functional-programming)
19-
- 1.4. [Design Patterns](#design-patterns)
20-
- 1.5. [Event Loop and Concurrency](#event-loop-and-concurrency)
17+
- 1.2. [Promises and Async/Await](./module-1.2-promises-and-async-await.md)
18+
- 1.3. [Functional Programming](./module-1.3-functional-programming.md)
19+
- 1.4. [Design Patterns](./module-1.4-design-patterns.md)
20+
- 1.5. [Event Loop and Concurrency](./module-1.5-event-loop-and-concurrency.md)
2121

22-
### [Module 2: Object-Oriented Programming (OOP) in JavaScript](#module-2-object-oriented-programming-oop-in-javascript)
22+
### [Module 2: Object-Oriented Programming (OOP) in JavaScript](./module-2-object-oriented-programming-oop-in-javascript.md)
2323

24-
2.1. [Constructors and Prototypes](#constructors-and-prototypes)
25-
2.2. [Classes and Inheritance](#classes-and-inheritance)
26-
2.3. [Mixins and Composition](#mixins-and-composition)
27-
2.4. [Private and Static Members](#private-and-static-members)
24+
- 2.1. [Constructors and Prototypes](./module-2.1-constructors-and-prototypes.md)
25+
- 2.2. [Classes and Inheritance](#classes-and-inheritance)
26+
- 2.3. [Mixins and Composition](#mixins-and-composition)
27+
- 2.4. [Private and Static Members](#private-and-static-members)
2828

2929
### [Module 3: Advanced DOM Manipulation](#module-3-advanced-dom-manipulation)
3030
3.1. [Traversing and Selecting DOM Elements](#traversing-and-selecting-dom-elements)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
### [Module 2: Object-Oriented Programming (OOP) in JavaScript](#module-2-object-oriented-programming-oop-in-javascript)
2+
3+
**Description:**
4+
5+
Module 2 is a comprehensive exploration of Object-Oriented Programming (OOP) in JavaScript. It delves into various OOP techniques, from the foundational concepts of constructors and prototypes to more advanced topics like classes, inheritance, mixins, composition, private members, and static members. By mastering these concepts, you'll gain the ability to structure your JavaScript code in a more organized, efficient, and maintainable way.
6+
7+
**Topics Covered:**
8+
9+
#### 2.1. Constructors and Prototypes
10+
11+
- **Constructors in JavaScript:** Constructors are special functions used to create and initialize objects in JavaScript. They act as blueprints for creating instances with shared properties and methods, promoting code reusability and structure.
12+
13+
**Example:**
14+
15+
```javascript
16+
function Person(name, age) {
17+
this.name = name;
18+
this.age = age;
19+
}
20+
21+
const john = new Person('John', 30);
22+
console.log(john.name); // Output: John
23+
```
24+
25+
- **Prototypes and Inheritance:** Prototypes allow objects to inherit properties and methods from other objects. This fundamental concept optimizes memory usage and facilitates the creation of complex object hierarchies.
26+
27+
**Example:**
28+
29+
```javascript
30+
Person.prototype.greet = function () {
31+
console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`);
32+
};
33+
34+
john.greet(); // Output: Hello, my name is John and I'm 30 years old.
35+
```
36+
37+
#### 2.2. Classes and Inheritance
38+
39+
- **Classes in JavaScript:** ES6 introduced class syntax, providing a structured way to create and manage objects. Classes encapsulate data and behavior, promoting code organization and readability.
40+
41+
**Example:**
42+
43+
```javascript
44+
class Animal {
45+
constructor(name) {
46+
this.name = name;
47+
}
48+
49+
speak() {
50+
console.log(`${this.name} makes a sound.`);
51+
}
52+
}
53+
54+
const dog = new Animal('Buddy');
55+
dog.speak(); // Output: Buddy makes a sound.
56+
```
57+
58+
- **Inheritance in ES6:** Inheritance is a key component of OOP, allowing classes to inherit properties and methods from other classes. ES6's `extends` keyword simplifies the inheritance process.
59+
60+
**Example:**
61+
62+
```javascript
63+
class Dog extends Animal {
64+
constructor(name, breed) {
65+
super(name);
66+
this.breed = breed;
67+
}
68+
69+
speak() {
70+
console.log(`${this.name} barks.`);
71+
}
72+
}
73+
74+
const goldenRetriever = new Dog('Max', 'Golden Retriever');
75+
goldenRetriever.speak(); // Output: Max barks.
76+
```
77+
78+
#### 2.3. Mixins and Composition
79+
80+
- **Mixins for Reusability:** Mixins are a way to share and reuse code among different objects or classes. They promote code modularity and flexibility.
81+
82+
- **Composing Objects:** Composition involves building complex objects by combining simpler ones. It's a versatile technique for creating reusable and structured code.
83+
84+
#### 2.4. Private and Static Members
85+
86+
- **Private Members:** Private members are properties or methods that are inaccessible from outside the class. Encapsulation of data and behavior is essential for creating robust and secure code.
87+
88+
- **Static Members:** Static members are associated with the class itself, not instances. They provide a way to create utility methods or properties that are shared among all instances of a class.
89+
90+
**Practical Application:**
91+
92+
The principles and techniques covered in this module are applied in various scenarios, including creating reusable libraries, structuring complex applications, and designing well-organized codebases. Private members and static members enhance code security and efficiency.
93+
94+
**Examples:**
95+
96+
The module includes practical examples and hands-on exercises for each topic. These examples provide opportunities to gain practical experience and deepen your understanding of OOP concepts in JavaScript.
97+
98+
**Key Takeaways:**
99+
100+
By mastering constructors, prototypes, classes, inheritance, mixins, composition, private members, and static members, you'll become proficient in structuring your JavaScript code in a way that promotes reusability, organization, and efficiency. These OOP techniques empower you to create maintainable and robust applications.
101+
102+
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#### 2.1. Constructors and Prototypes
2+
3+
Object-Oriented Programming (OOP) in JavaScript often begins with understanding Constructors and Prototypes. These concepts are fundamental to creating and managing objects.
4+
5+
##### Constructors in JavaScript
6+
7+
In JavaScript, constructors are functions used to create and initialize objects. They serve as templates for creating objects with shared properties and methods. Here's an example of a simple constructor for creating `Person` objects:
8+
9+
```javascript
10+
function Person(name, age) {
11+
this.name = name;
12+
this.age = age;
13+
}
14+
15+
const john = new Person('John', 30);
16+
console.log(john.name); // Output: John
17+
```
18+
19+
In this example, we define a `Person` constructor that takes `name` and `age` as parameters. When we create a new `Person` object using `new`, it initializes the object's properties based on the constructor.
20+
21+
##### Prototypes and Inheritance
22+
23+
Prototypes allow objects to inherit properties and methods from other objects. Understanding prototypes is crucial for creating efficient and maintainable code in JavaScript.
24+
25+
```javascript
26+
// Creating a prototype method
27+
Person.prototype.greet = function () {
28+
console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`);
29+
};
30+
31+
john.greet(); // Output: Hello, my name is John and I'm 30 years old.
32+
```
33+
34+
In this example, we add a `greet` method to the `Person` prototype, which all `Person` objects can access. This method is shared among all instances of `Person`, making it more memory-efficient.
35+
36+
#### Constructor Functions and `new`
37+
38+
- Constructor functions, like `Person` in the example, are named with capital letters by convention.
39+
- The `new` keyword is used to create instances of objects based on the constructor.
40+
- Inside the constructor, `this` refers to the new instance being created.
41+
- Properties and methods specific to an object are set using `this.propertyName` inside the constructor.
42+
43+
#### Prototype Chain and Inheritance
44+
45+
- When you add methods or properties to a constructor's prototype, they are shared among all objects created from that constructor.
46+
- Objects can access properties and methods defined in their constructor's prototype, creating an inheritance mechanism.
47+
- This promotes code reusability and efficient memory usage.
48+
49+
#### Best Practices
50+
51+
- Use constructor functions for creating multiple instances of objects with shared properties and methods.
52+
- Place methods shared among instances in the constructor's prototype to optimize memory usage.
53+
- Follow naming conventions, such as capitalizing the first letter of constructor functions, for clarity.
54+
55+
Understanding Constructors and Prototypes is foundational to mastering Object-Oriented Programming in JavaScript. They enable you to create organized, efficient, and maintainable code.

module-2.2-classes-and-inheritance.md

Whitespace-only changes.

0 commit comments

Comments
 (0)