|
732 | 732 |
|
733 | 733 | **[⬆ Back to Top](#table-of-contents)**
|
734 | 734 |
|
735 |
| -3. ### What is the difference between Call, Apply and Bind |
| 735 | +3. ### What is the Difference Between `call`, `apply`, and `bind` |
736 | 736 |
|
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. |
738 | 738 |
|
739 |
| - **Call:** The call() method invokes a function with a given `this` value and arguments provided one by one |
| 739 | + --- |
740 | 740 |
|
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 |
742 | 753 | var employee1 = { firstName: "John", lastName: "Rodson" };
|
743 | 754 | var employee2 = { firstName: "Jimmy", lastName: "Baily" };
|
744 | 755 |
|
745 | 756 | function invite(greeting1, greeting2) {
|
746 | 757 | console.log(
|
747 |
| - greeting1 + |
748 |
| - " " + |
749 |
| - this.firstName + |
750 |
| - " " + |
751 |
| - this.lastName + |
752 |
| - ", " + |
753 |
| - greeting2 |
| 758 | + greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2 |
754 | 759 | );
|
755 | 760 | }
|
756 | 761 |
|
757 | 762 | invite.call(employee1, "Hello", "How are you?"); // Hello John Rodson, How are you?
|
758 | 763 | invite.call(employee2, "Hello", "How are you?"); // Hello Jimmy Baily, How are you?
|
759 | 764 | ```
|
760 | 765 |
|
761 |
| - **Apply:** Invokes the function with a given `this` value and allows you to pass in arguments as an array |
| 766 | + --- |
762 | 767 |
|
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 |
764 | 780 | var employee1 = { firstName: "John", lastName: "Rodson" };
|
765 | 781 | var employee2 = { firstName: "Jimmy", lastName: "Baily" };
|
766 | 782 |
|
767 | 783 | function invite(greeting1, greeting2) {
|
768 | 784 | console.log(
|
769 |
| - greeting1 + |
770 |
| - " " + |
771 |
| - this.firstName + |
772 |
| - " " + |
773 |
| - this.lastName + |
774 |
| - ", " + |
775 |
| - greeting2 |
| 785 | + greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2 |
776 | 786 | );
|
777 | 787 | }
|
778 | 788 |
|
779 | 789 | invite.apply(employee1, ["Hello", "How are you?"]); // Hello John Rodson, How are you?
|
780 | 790 | invite.apply(employee2, ["Hello", "How are you?"]); // Hello Jimmy Baily, How are you?
|
781 | 791 | ```
|
782 | 792 |
|
783 |
| - **Bind:** returns a new function, allowing you to pass any number of arguments |
| 793 | + --- |
784 | 794 |
|
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 |
786 | 807 | var employee1 = { firstName: "John", lastName: "Rodson" };
|
787 | 808 | var employee2 = { firstName: "Jimmy", lastName: "Baily" };
|
788 | 809 |
|
789 | 810 | function invite(greeting1, greeting2) {
|
790 | 811 | console.log(
|
791 |
| - greeting1 + |
792 |
| - " " + |
793 |
| - this.firstName + |
794 |
| - " " + |
795 |
| - this.lastName + |
796 |
| - ", " + |
797 |
| - greeting2 |
| 812 | + greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2 |
798 | 813 | );
|
799 | 814 | }
|
800 | 815 |
|
801 | 816 | var inviteEmployee1 = invite.bind(employee1);
|
802 | 817 | var inviteEmployee2 = invite.bind(employee2);
|
| 818 | +
|
803 | 819 | inviteEmployee1("Hello", "How are you?"); // Hello John Rodson, How are you?
|
804 | 820 | inviteEmployee2("Hello", "How are you?"); // Hello Jimmy Baily, How are you?
|
805 | 821 | ```
|
806 | 822 |
|
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 | + --- |
808 | 824 |
|
809 |
| - Bind creates a new function that will have `this` set to the first parameter passed to bind(). |
| 825 | + #### Summary |
810 | 826 |
|
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)** |
812 | 846 |
|
813 | 847 | 4. ### What is JSON and its common operations
|
814 | 848 |
|
|
0 commit comments