Skip to content

Commit 0e139f9

Browse files
committed
Simplify prototype chain and promises concepts
1 parent aaa0b13 commit 0e139f9

File tree

1 file changed

+53
-20
lines changed

1 file changed

+53
-20
lines changed

README.md

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -705,10 +705,29 @@
705705

706706
2. ### What is a prototype chain
707707

708-
The **Prototype chain** is a fundamental inheritance mechanism that enables objects to access properties and methods from other objects. When a property or method is accessed on an object, JavaScript first checks if it exists directly on that object. If not, it traverses up the prototype chain—following the object's internal **[[Prototype]]** reference—until it either finds the requested member or reaches the end of the chain (typically null)
708+
The prototype chain is a core concept in JavaScript’s inheritance model. It allows objects to inherit properties and methods from other objects. When you try to access a property or method on an object, JavaScript first looks for it on that object itself. If it’s not found, the engine looks up the object's internal `[[Prototype]]` reference (accessible via `Object.getPrototypeOf(obj)` or the deprecated `__proto__` property) and continues searching up the chain until it finds the property or reaches the end (usually `null`).
709709
710-
The prototype on object instance is available through **Object.getPrototypeOf(object)** or **\_\_proto\_\_** property whereas prototype on constructor function is available through **Object.prototype**.
710+
For objects created via constructor functions, the prototype chain starts with the instance, then refers to the constructor’s `.prototype` object, and continues from there. For example:
711711
712+
```javascript
713+
function Person() {}
714+
const person1 = new Person();
715+
716+
console.log(Object.getPrototypeOf(person1) === Person.prototype); // true
717+
```
718+
719+
This mechanism allows for property and method sharing among objects, enabling code reuse and a form of inheritance.
720+
721+
**Summary:**
722+
723+
* The prototype chain enables inheritance in JavaScript.
724+
* If a property isn’t found on an object, JavaScript looks up its prototype chain.
725+
* The prototype of an object instance can be accessed with `Object.getPrototypeOf(obj)` or `__proto__`.
726+
* The prototype of a constructor function is available via `Constructor.prototype`.
727+
* The chain ends when the prototype is `null`.
728+
729+
The prototype chain among objects appears as below,
730+
712731
![Screenshot](images/prototype_chain.png)
713732
714733
**[⬆ Back to Top](#table-of-contents)**
@@ -1617,31 +1636,39 @@
16171636
**[⬆ Back to Top](#table-of-contents)**
16181637
16191638
52. ### What is a promise
1639+
A **Promise** is a JavaScript object that represents the eventual completion (or failure) of an asynchronous operation and its resulting value. It acts as a placeholder for a value that may not be available yet but will be resolved in the future.
16201640
1621-
A promise is an object that may produce a single value some time in the future with either a resolved value or a reason that it’s not resolved (i.e. rejected, that means for example, due to a network error). It will be in one of the 3 possible states: fulfilled, rejected, or pending.
1641+
A Promise can be in one of **three states**:
1642+
- `pending`: Initial state, neither fulfilled nor rejected.
1643+
- `fulfilled`: The operation completed successfully.
1644+
- `rejected`: The operation failed (e.g., due to a network error).
16221645
1623-
The syntax of Promise creation looks like below,
1646+
1647+
#### Promise Syntax
16241648
16251649
```javascript
16261650
const promise = new Promise(function (resolve, reject) {
1627-
// promise description
1651+
// Perform async operation
16281652
});
16291653
```
1630-
1631-
The usage of a promise would be as below,
1632-
1654+
#### Example: Creating and Using a Promise
16331655
```javascript
1634-
const promise = new Promise(
1635-
(resolve) => {
1636-
setTimeout(() => {
1637-
resolve("I'm a Promise!");
1638-
}, 5000);
1639-
},
1640-
(reject) => {}
1641-
);
1642-
1643-
promise.then((value) => console.log(value));
1656+
const promise = new Promise((resolve, reject) => {
1657+
setTimeout(() => {
1658+
resolve("I'm a Promise!");
1659+
}, 5000);
1660+
});
1661+
1662+
promise
1663+
.then((value) => console.log(value)); // Logs after 5 seconds: "I'm a Promise!"
1664+
.catch((error) => console.error(error)) // Handles any rejection
1665+
.finally(() => console.log("Done")); // Runs regardless of success or failure
16441666
```
1667+
In the above example:
1668+
1669+
* A `Promise` is created to handle an asynchronous operation with `resolve` and `reject` callbacks.
1670+
* The `setTimeout` resolves the promise with a value after 5 seconds.
1671+
* `.then()`, `.catch()`, and `.finally()` are used to handle success, errors, and cleanup respectively.
16451672
16461673
The action flow of a promise will be as below,
16471674
@@ -1650,12 +1677,18 @@
16501677
**[⬆ Back to Top](#table-of-contents)**
16511678
16521679
53. ### Why do you need a promise
1680+
Promises are **used to handle asynchronous operations**, especially in languages like JavaScript, which often work with non-blocking operations such as network requests, file I/O, and timers. When an operation is asynchronous, it doesn't immediately return a result; instead, it works in the background and provides the result later. Handling this in a clean, organized way can be difficult without a structured approach.
1681+
1682+
Promises are used to:
16531683

1654-
Promises are used to handle asynchronous operations. They provide an alternative approach for callbacks by reducing the callback hell and writing the cleaner code.
1684+
1. **Handle asynchronous operations**.
1685+
2. **Provide a cleaner alternative to callbacks**.
1686+
3. **Avoid callback hell**.
1687+
4. **Make code more readable and maintainable**.
16551688

16561689
**[⬆ Back to Top](#table-of-contents)**
16571690

1658-
54. ### What are the three states of promise
1691+
54. ### Explain the three states of promise
16591692

16601693
Promises have three states:
16611694

0 commit comments

Comments
 (0)