Skip to content

Commit fb71741

Browse files
committed
Improve higher order function answer
1 parent 2182bbe commit fb71741

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

README.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@
684684

685685
2. ### What is a prototype chain
686686

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)
688688
689689
The prototype on object instance is available through **Object.getPrototypeOf(object)** or **\_\_proto\_\_** property whereas prototype on constructor function is available through **Object.prototype**.
690690
@@ -922,19 +922,29 @@
922922

923923
13. ### What is a higher order function
924924

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.
927926

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.
934944

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.
936946

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.
938948

939949
**[⬆ Back to Top](#table-of-contents)**
940950

0 commit comments

Comments
 (0)