Skip to content

Commit f7ae59b

Browse files
committed
Improve call, apply & bind details
1 parent c726af5 commit f7ae59b

File tree

1 file changed

+66
-32
lines changed

1 file changed

+66
-32
lines changed

README.md

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -732,83 +732,117 @@
732732
733733
**[⬆ Back to Top](#table-of-contents)**
734734
735-
3. ### What is the difference between Call, Apply and Bind
735+
3. ### What is the Difference Between `call`, `apply`, and `bind`
736736
737-
The difference between Call, Apply and Bind can be explained with below examples,
737+
In JavaScript, `call`, `apply`, and `bind` are methods that allow you to control the context (`this` value) in which a function is executed. While their purposes are similar, they differ in how they handle arguments and when the function is invoked.
738738
739-
**Call:** The call() method invokes a function with a given `this` value and arguments provided one by one
739+
---
740740
741-
```javascript
741+
#### `call`
742+
743+
- **Description:**
744+
The `call()` method invokes a function immediately, allowing you to specify the value of `this` and pass arguments individually (comma-separated).
745+
746+
- **Syntax:**
747+
```js
748+
func.call(thisArg, arg1, arg2, ...)
749+
```
750+
751+
- **Example:**
752+
```js
742753
var employee1 = { firstName: "John", lastName: "Rodson" };
743754
var employee2 = { firstName: "Jimmy", lastName: "Baily" };
744755
745756
function invite(greeting1, greeting2) {
746757
console.log(
747-
greeting1 +
748-
" " +
749-
this.firstName +
750-
" " +
751-
this.lastName +
752-
", " +
753-
greeting2
758+
greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2
754759
);
755760
}
756761
757762
invite.call(employee1, "Hello", "How are you?"); // Hello John Rodson, How are you?
758763
invite.call(employee2, "Hello", "How are you?"); // Hello Jimmy Baily, How are you?
759764
```
760765
761-
**Apply:** Invokes the function with a given `this` value and allows you to pass in arguments as an array
766+
---
762767
763-
```javascript
768+
#### `apply`
769+
770+
- **Description:**
771+
The `apply()` method is similar to `call()`, but it takes the function arguments as an array (or array-like object) instead of individual arguments.
772+
773+
- **Syntax:**
774+
```js
775+
func.apply(thisArg, [argsArray])
776+
```
777+
778+
- **Example:**
779+
```js
764780
var employee1 = { firstName: "John", lastName: "Rodson" };
765781
var employee2 = { firstName: "Jimmy", lastName: "Baily" };
766782
767783
function invite(greeting1, greeting2) {
768784
console.log(
769-
greeting1 +
770-
" " +
771-
this.firstName +
772-
" " +
773-
this.lastName +
774-
", " +
775-
greeting2
785+
greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2
776786
);
777787
}
778788
779789
invite.apply(employee1, ["Hello", "How are you?"]); // Hello John Rodson, How are you?
780790
invite.apply(employee2, ["Hello", "How are you?"]); // Hello Jimmy Baily, How are you?
781791
```
782792
783-
**Bind:** returns a new function, allowing you to pass any number of arguments
793+
---
784794
785-
```javascript
795+
#### `bind`
796+
797+
- **Description:**
798+
The `bind()` method creates a new function with a specific `this` value and, optionally, preset initial arguments. Unlike `call` and `apply`, `bind` does **not** immediately invoke the function; instead, it returns a new function that you can call later.
799+
800+
- **Syntax:**
801+
```js
802+
var boundFunc = func.bind(thisArg[, arg1[, arg2[, ...]]])
803+
```
804+
805+
- **Example:**
806+
```js
786807
var employee1 = { firstName: "John", lastName: "Rodson" };
787808
var employee2 = { firstName: "Jimmy", lastName: "Baily" };
788809
789810
function invite(greeting1, greeting2) {
790811
console.log(
791-
greeting1 +
792-
" " +
793-
this.firstName +
794-
" " +
795-
this.lastName +
796-
", " +
797-
greeting2
812+
greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2
798813
);
799814
}
800815
801816
var inviteEmployee1 = invite.bind(employee1);
802817
var inviteEmployee2 = invite.bind(employee2);
818+
803819
inviteEmployee1("Hello", "How are you?"); // Hello John Rodson, How are you?
804820
inviteEmployee2("Hello", "How are you?"); // Hello Jimmy Baily, How are you?
805821
```
806822
807-
Call and Apply are pretty much interchangeable. Both execute the current function immediately. You need to decide whether it’s easier to send in an array or a comma separated list of arguments. You can remember by treating Call is for **comma** (separated list) and Apply is for **Array**.
823+
---
808824
809-
Bind creates a new function that will have `this` set to the first parameter passed to bind().
825+
#### Summary
810826
811-
**[⬆ Back to Top](#table-of-contents)**
827+
| Method | Invokes Function Immediately? | How Arguments Are Passed | Returns |
828+
|--------|-------------------------------|----------------------------------|--------------|
829+
| `call` | Yes | Comma-separated list | Function's result |
830+
| `apply`| Yes | Array or array-like object | Function's result |
831+
| `bind` | No | (Optional) preset, then rest | New function |
832+
833+
---
834+
835+
## Key Points
836+
837+
- **`call`** and **`apply`** are almost interchangeable; both invoke the function immediately, but differ in how arguments are passed.
838+
- _Tip:_ "Call is for Comma-separated, Apply is for Array."
839+
- **`bind`** does not execute the function immediately. Instead, it creates a new function with the specified `this` value and optional arguments, which can be called later.
840+
841+
- Use `call` or `apply` when you want to immediately invoke a function with a specific `this` context. Use `bind` when you want to create a new function with a specific `this` context to be invoked later.
842+
843+
---
844+
845+
**[⬆ Back to Top](#table-of-contents)**
812846
813847
4. ### What is JSON and its common operations
814848

0 commit comments

Comments
 (0)