|
705 | 705 |
|
706 | 706 | 2. ### What is a prototype chain
|
707 | 707 |
|
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`). |
709 | 709 |
|
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: |
711 | 711 |
|
| 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 | + |
712 | 731 | 
|
713 | 732 |
|
714 | 733 | **[⬆ Back to Top](#table-of-contents)**
|
|
1617 | 1636 | **[⬆ Back to Top](#table-of-contents)**
|
1618 | 1637 |
|
1619 | 1638 | 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. |
1620 | 1640 |
|
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). |
1622 | 1645 |
|
1623 |
| - The syntax of Promise creation looks like below, |
| 1646 | +
|
| 1647 | + #### Promise Syntax |
1624 | 1648 |
|
1625 | 1649 | ```javascript
|
1626 | 1650 | const promise = new Promise(function (resolve, reject) {
|
1627 |
| - // promise description |
| 1651 | + // Perform async operation |
1628 | 1652 | });
|
1629 | 1653 | ```
|
1630 |
| -
|
1631 |
| - The usage of a promise would be as below, |
1632 |
| -
|
| 1654 | + #### Example: Creating and Using a Promise |
1633 | 1655 | ```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 |
1644 | 1666 | ```
|
| 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. |
1645 | 1672 |
|
1646 | 1673 | The action flow of a promise will be as below,
|
1647 | 1674 |
|
|
1650 | 1677 | **[⬆ Back to Top](#table-of-contents)**
|
1651 | 1678 |
|
1652 | 1679 | 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: |
1653 | 1683 |
|
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**. |
1655 | 1688 |
|
1656 | 1689 | **[⬆ Back to Top](#table-of-contents)**
|
1657 | 1690 |
|
1658 |
| -54. ### What are the three states of promise |
| 1691 | +54. ### Explain the three states of promise |
1659 | 1692 |
|
1660 | 1693 | Promises have three states:
|
1661 | 1694 |
|
|
0 commit comments