JavaScript call() Method
Subject: JavaScript
The call() method in JavaScript allows you to invoke a function with a specified this value and arguments passed individually. It is part of the Function prototype, so all functions can access it. This method is useful for borrowing functions from other objects.
Syntax
- functionName: The function to invoke.
 - thisArg: The value to use as this inside the function.
 - arg1, arg2, ...: Arguments passed to the function.
 
Example 1: Using call() to Set this
Explanation:
- Inside fullName, this refers to user, not person.
 - The method is borrowed using call().
 
Example 2: Passing Arguments
Explanation:
- greet is called with this set to user.
 - Arguments "Hello" and "!" are passed individually.
 
Difference Between call() and apply()
- call() passes arguments one by one.
 - apply() passes arguments as an array.
 
Example 3: Reusing a Function
Key Takeaways
- call() invokes a function with a specified this value.
 - Useful to borrow methods from one object for another.
 - Arguments are passed individually.
 - call() is related to apply() and bind() methods.