Skip to content

Commit 806bf78

Browse files
add new notes
1 parent bf6a94d commit 806bf78

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2833
-1400
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
1-
### OOPs in JavaScript
2-
3-
In JavaScript, OOPs is based on the concept of prototypes, which is different from class-based OOPs in other languages like Java and C++.
4-
5-
In JavaScript, every object has a prototype object, which acts as a template for the object. When a property or method is accessed on an object, JavaScript first looks for that property or method on the object itself. If it's not found, JavaScript looks for it on the object's prototype. If it's still not found, JavaScript continues up the prototype chain until it reaches the Object.prototype, which is the root of the prototype chain.
6-
7-
To create an object in JavaScript, we can use either the object literal notation or the Object() constructor. For example, here's how we can create an object using the object literal notation:
8-
9-
```javascript
10-
const person = {
11-
name: 'Alice',
12-
age: 30,
13-
gender: 'female',
14-
walk() {
15-
console.log(`${this.name} is walking.`);
16-
},
17-
talk() {
18-
console.log(`${this.name} is talking.`);
19-
}
20-
};
21-
22-
```
23-
24-
In this example, we create an object called "person" that has properties for the person's name, age, and gender, as well as methods for walking and talking.
25-
26-
We can also create objects using constructor functions, which act as blueprints for creating objects with shared properties and methods. For example:
27-
28-
```javascript
29-
function Person(name, age, gender) {
30-
this.name = name;
31-
this.age = age;
32-
this.gender = gender;
33-
}
34-
35-
Person.prototype.walk = function() {
36-
console.log(`${this.name} is walking.`);
37-
};
38-
39-
Person.prototype.talk = function() {
40-
console.log(`${this.name} is talking.`);
41-
};
42-
43-
const person1 = new Person('Alice', 30, 'female');
44-
const person2 = new Person('Bob', 40, 'male');
45-
46-
person1.walk(); // logs "Alice is walking."
47-
person2.talk(); // logs "Bob is talking."
48-
```
49-
50-
In this example, we define a constructor function called "Person" that takes in arguments for the person's name, age, and gender, and sets those properties on the newly created object using this. We then add the walk() and talk() methods to the Person.prototype object, which will be shared by all instances of the "Person" object.
51-
52-
Overall, JavaScript's approach to OOPs is based on prototypes rather than classes, which can take some getting used to if you are coming from a class-based OOPs language. However, once you understand the concept of prototypes and how they work, you can create powerful and flexible object-oriented code in JavaScript.
1+
### OOPs in JavaScript
2+
3+
In JavaScript, OOPs is based on the concept of prototypes, which is different from class-based OOPs in other languages like Java and C++.
4+
5+
In JavaScript, every object has a prototype object, which acts as a template for the object. When a property or method is accessed on an object, JavaScript first looks for that property or method on the object itself. If it's not found, JavaScript looks for it on the object's prototype. If it's still not found, JavaScript continues up the prototype chain until it reaches the Object.prototype, which is the root of the prototype chain.
6+
7+
To create an object in JavaScript, we can use either the object literal notation or the Object() constructor. For example, here's how we can create an object using the object literal notation:
8+
9+
```javascript
10+
const person = {
11+
name: 'Alice',
12+
age: 30,
13+
gender: 'female',
14+
walk() {
15+
console.log(`${this.name} is walking.`);
16+
},
17+
talk() {
18+
console.log(`${this.name} is talking.`);
19+
}
20+
};
21+
22+
```
23+
24+
In this example, we create an object called "person" that has properties for the person's name, age, and gender, as well as methods for walking and talking.
25+
26+
We can also create objects using constructor functions, which act as blueprints for creating objects with shared properties and methods. For example:
27+
28+
```javascript
29+
function Person(name, age, gender) {
30+
this.name = name;
31+
this.age = age;
32+
this.gender = gender;
33+
}
34+
35+
Person.prototype.walk = function() {
36+
console.log(`${this.name} is walking.`);
37+
};
38+
39+
Person.prototype.talk = function() {
40+
console.log(`${this.name} is talking.`);
41+
};
42+
43+
const person1 = new Person('Alice', 30, 'female');
44+
const person2 = new Person('Bob', 40, 'male');
45+
46+
person1.walk(); // logs "Alice is walking."
47+
person2.talk(); // logs "Bob is talking."
48+
```
49+
50+
In this example, we define a constructor function called "Person" that takes in arguments for the person's name, age, and gender, and sets those properties on the newly created object using this. We then add the walk() and talk() methods to the Person.prototype object, which will be shared by all instances of the "Person" object.
51+
52+
Overall, JavaScript's approach to OOPs is based on prototypes rather than classes, which can take some getting used to if you are coming from a class-based OOPs language. However, once you understand the concept of prototypes and how they work, you can create powerful and flexible object-oriented code in JavaScript.

notes/English/51-OOPs.md

-56
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
1-
### Prototypal inheritance and prototype chain
2-
3-
Prototypal inheritance is a mechanism in JavaScript that allows objects to inherit properties and methods from other objects, known as their prototypes. When a property or method is accessed on an object, JavaScript first looks for it directly on the object, and if it's not found, it looks for it on the object's prototype, and then on the prototype's prototype, and so on, following the prototype chain until the property or method is found or until the end of the chain is reached (which is the object `Object.prototype`, the final fallback).
4-
5-
The prototype chain is the chain of prototypes that an object follows to access properties and methods. Each object has an internal property called `[[Prototype]]` (also accessible via the `__proto__` property) that points to its prototype object. When an object is created using object literals, the default prototype is `Object.prototype`. However, you can create custom prototypes by defining constructor functions and setting their `prototype` property to an object that will be used as the prototype for the objects created with that constructor function.
6-
7-
8-
### Prototypal inheritance on Built-in objects in JavaScript
9-
10-
Prototypal inheritance is a fundamental concept in JavaScript that allows objects to inherit properties and methods from other objects. In the case of built-in objects, such as arrays or strings, they inherit their properties and methods from their prototype object. This means that any instance of an array, for example, will have access to all the properties and methods defined on the Array.prototype object. Similarly, instances of strings will have access to the properties and methods defined on the String.prototype object. This feature makes JavaScript highly flexible and extensible.
11-
12-
### Difference between Inheritance and classes
13-
14-
In JavaScript, classes are blueprints for creating objects with specific properties and methods. Inheritance, on the other hand, is a mechanism that allows an object to inherit properties and methods from another object.
15-
16-
Classes provide a way to define objects with similar behavior and characteristics in a reusable way. They can be used to create multiple instances of an object with their own unique characteristics.
17-
18-
Inheritance allows objects to share properties and methods with other objects, which can simplify code and avoid repetition. It provides a way to create new objects based on existing ones, inheriting their properties and methods while also adding or modifying them as needed.
19-
20-
In summary, classes are used to define objects while inheritance is a mechanism for sharing properties and methods between objects.
21-
22-
23-
### How to use constructor functions ?
24-
25-
Constructor functions in JavaScript are used to create objects with shared properties and methods. Here's an example of how to use a constructor function:
26-
27-
1. Define the constructor function:
28-
```
29-
function Person(name, age) {
30-
this.name = name;
31-
this.age = age;
32-
}
33-
```
34-
35-
2. Create new instances of the object using the `new` keyword:
36-
```
37-
let person1 = new Person('John', 30);
38-
let person2 = new Person('Jane', 25);
39-
```
40-
41-
3. Access the object's properties using dot notation:
42-
```
43-
console.log(person1.name); // Output: John
44-
console.log(person2.age); // Output: 25
45-
```
46-
47-
You can also add methods to the constructor function's prototype, which will be shared by all instances of the object:
48-
```
49-
Person.prototype.sayHello = function() {
50-
console.log(`Hello, my name is ${this.name}.`);
51-
}
52-
53-
person1.sayHello(); // Output: Hello, my name is John.
54-
person2.sayHello(); // Output: Hello, my name is Jane.
55-
```
1+
### Prototypal inheritance and prototype chain
2+
3+
Prototypal inheritance is a mechanism in JavaScript that allows objects to inherit properties and methods from other objects, known as their prototypes. When a property or method is accessed on an object, JavaScript first looks for it directly on the object, and if it's not found, it looks for it on the object's prototype, and then on the prototype's prototype, and so on, following the prototype chain until the property or method is found or until the end of the chain is reached (which is the object `Object.prototype`, the final fallback).
4+
5+
The prototype chain is the chain of prototypes that an object follows to access properties and methods. Each object has an internal property called `[[Prototype]]` (also accessible via the `__proto__` property) that points to its prototype object. When an object is created using object literals, the default prototype is `Object.prototype`. However, you can create custom prototypes by defining constructor functions and setting their `prototype` property to an object that will be used as the prototype for the objects created with that constructor function.
6+
7+
8+
### Prototypal inheritance on Built-in objects in JavaScript
9+
10+
Prototypal inheritance is a fundamental concept in JavaScript that allows objects to inherit properties and methods from other objects. In the case of built-in objects, such as arrays or strings, they inherit their properties and methods from their prototype object. This means that any instance of an array, for example, will have access to all the properties and methods defined on the Array.prototype object. Similarly, instances of strings will have access to the properties and methods defined on the String.prototype object. This feature makes JavaScript highly flexible and extensible.
11+
12+
### Difference between Inheritance and classes
13+
14+
In JavaScript, classes are blueprints for creating objects with specific properties and methods. Inheritance, on the other hand, is a mechanism that allows an object to inherit properties and methods from another object.
15+
16+
Classes provide a way to define objects with similar behavior and characteristics in a reusable way. They can be used to create multiple instances of an object with their own unique characteristics.
17+
18+
Inheritance allows objects to share properties and methods with other objects, which can simplify code and avoid repetition. It provides a way to create new objects based on existing ones, inheriting their properties and methods while also adding or modifying them as needed.
19+
20+
In summary, classes are used to define objects while inheritance is a mechanism for sharing properties and methods between objects.
21+
22+
23+
### How to use constructor functions ?
24+
25+
Constructor functions in JavaScript are used to create objects with shared properties and methods. Here's an example of how to use a constructor function:
26+
27+
1. Define the constructor function:
28+
```
29+
function Person(name, age) {
30+
this.name = name;
31+
this.age = age;
32+
}
33+
```
34+
35+
2. Create new instances of the object using the `new` keyword:
36+
```
37+
let person1 = new Person('John', 30);
38+
let person2 = new Person('Jane', 25);
39+
```
40+
41+
3. Access the object's properties using dot notation:
42+
```
43+
console.log(person1.name); // Output: John
44+
console.log(person2.age); // Output: 25
45+
```
46+
47+
You can also add methods to the constructor function's prototype, which will be shared by all instances of the object:
48+
```
49+
Person.prototype.sayHello = function() {
50+
console.log(`Hello, my name is ${this.name}.`);
51+
}
52+
53+
person1.sayHello(); // Output: Hello, my name is John.
54+
person2.sayHello(); // Output: Hello, my name is Jane.
55+
```

0 commit comments

Comments
 (0)