|
684 | 684 |
|
685 | 685 | 2. ### What is a prototype chain
|
686 | 686 |
|
687 |
| - **Prototype chaining** is used to build new types of objects based on existing ones. It is similar to inheritance in a class based language. i.e, When you create an object using a constructor function or a class, the created object inherits properties from a prototype object. |
| 687 | + 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) |
688 | 688 |
|
689 | 689 | The prototype on object instance is available through **Object.getPrototypeOf(object)** or **\_\_proto\_\_** property whereas prototype on constructor function is available through **Object.prototype**.
|
690 | 690 |
|
|
922 | 922 |
|
923 | 923 | 13. ### What is a higher order function
|
924 | 924 |
|
925 |
| - A higher-order function is a function that accepts another function as an argument or returns a function as a return value or both. |
926 |
| - The syntactic structure of higher order function will be as follows, |
| 925 | + A higher-order function is a function that either accepts another function as an argument, returns a function as its result, or both. This concept is a core part of JavaScript's functional programming capabilities and is widely used for creating modular, reusable, and expressive code. |
927 | 926 |
|
928 |
| - ```javascript |
929 |
| - const firstOrderFunc = () => |
930 |
| - console.log("Hello, I am a First order function"); |
931 |
| - const higherOrder = (ReturnFirstOrderFunc) => ReturnFirstOrderFunc(); |
932 |
| - higherOrder(firstOrderFunc); |
933 |
| - ``` |
| 927 | + The syntactic structure of higher order function will be explained with an example as follows, |
| 928 | + |
| 929 | + ```javascript |
| 930 | + // First-order function (does not accept or return another function) |
| 931 | + const firstOrderFunc = () => |
| 932 | + console.log("Hello, I am a first-order function"); |
| 933 | + |
| 934 | + // Higher-order function (accepts a function as an argument) |
| 935 | + const higherOrder = (callback) => callback(); |
| 936 | + |
| 937 | + // Passing the first-order function to the higher-order function |
| 938 | + higherOrder(firstOrderFunc); |
| 939 | + ``` |
| 940 | + |
| 941 | + In this example: |
| 942 | + |
| 943 | + 1. `firstOrderFunc` is a regular (first-order) function. |
934 | 944 |
|
935 |
| - You can also call the function which you are passing to higher order function as callback function. |
| 945 | + 2. `higherOrder` is a higher-order function because it takes another function as an argument. |
936 | 946 |
|
937 |
| - The higher order function is helpful to write the modular and reusable code. |
| 947 | + 3. `firstOrderFunc` is also called a **callback function** because it is passed to and executed by another function. |
938 | 948 |
|
939 | 949 | **[⬆ Back to Top](#table-of-contents)**
|
940 | 950 |
|
|
0 commit comments