-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
call()
Method – Function Invocation with Custom ContextThe call()
method lets you invoke a function, explicitly setting the value of this
and passing arguments one by one.
func.call(thisArg, arg1, arg2, ...);
thisArg
: Value to use as this
inside the function.
arg1, arg2, ...
: Arguments passed individually.
function greet(city) {
console.log(`Hello ${this.name} from ${city}`);
}
const person = { name: "Ravi" };
greet.call(person, "Delhi");
// Output: Hello Ravi from Delhi
const person1 = {
name: "Alice",
greet: function () {
console.log("Hi " + this.name);
},
};
const person2 = { name: "Bob" };
person1.greet.call(person2); // Output: Hi Bob
Feature | call() | apply() |
---|---|---|
Syntax | func.call(this, arg1, arg2) |
func.apply(this, [arg1, arg2]) |
Arguments | Passed individually | Passed as array |
Q1. Write a function sayName(greeting)
that prints "greeting
, my name is [this.name]". Use call()
with { name: "Anjali" }
and "Hello"
as argument.
Q2. Create a function multiply(x, y)
and call it using call()
with null
as this
and numbers 4
and 5
as arguments.
Q3. Create two objects a
and b
. Let a
have a method show()
that prints this.value
. Use call()
to run a.show
using b
’s context.
Q4. Define a function introduce(city, country)
and call it with call()
using { name: "Meena" }
and arguments "Mumbai", "India"
. Print a full sentence.
Q5. Create a function area(length, width)
and invoke it with call()
to calculate and log area of a rectangle.
Q6. Write a function describeRole()
that logs this.name + " is a " + this.role
. Use call()
with two different objects.
Q7. Use a standalone function showBrand()
and invoke it with call()
using an object { brand: "Nike" }
to print the brand.
Q8. Write a function sum(a, b, c)
and use call()
to pass the values 2, 3, 4
and log the result.
Q9. Create a method showDetails()
in object user1
and use call()
to reuse it for user2
. Both objects should have name
and age
.
Q10. Create a function combine(first, second)
and use call()
with an object { separator: " & " }
to print "first & second
".
Q1: What is the correct syntax for using call()?
Q2: What does the call() method do?
Q3: What is the main difference between call() and apply()?
Q4: Which of the following best describes method borrowing using call()?
Q5: What happens when you call a function using call() with null as this?
Q6: Which is a valid use-case of call()?