0% found this document useful (0 votes)
26 views9 pages

JavaScript Function Call Method

The document explains the JavaScript call() method, which allows a method to be invoked on different objects, enabling method reuse. It details how the 'this' keyword works in various contexts and provides examples of using call() to invoke methods with different objects and arguments. Additionally, it emphasizes that all functions in JavaScript are methods of objects.

Uploaded by

samerguda13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views9 pages

JavaScript Function Call Method

The document explains the JavaScript call() method, which allows a method to be invoked on different objects, enabling method reuse. It details how the 'this' keyword works in various contexts and provides examples of using call() to invoke methods with different objects and arguments. Additionally, it emphasizes that all functions in JavaScript are methods of objects.

Uploaded by

samerguda13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

 Tutorials  Exercises  Services   Get Certified Sign U

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS

JavaScript Function call()


‹ Previous Next ›

Method Reuse
With the call() method, you can write a method that can be used on
different objects.

All Functions are Methods


In JavaScript all functions are object methods.

If a function is not a method of a JavaScript object, it is a function of the


global object (see previous chapter).

The example below creates an object with 3 properties, firstName,


lastName, fullName.

Example

const person = {
firstName:"John",

https://www.w3schools.com/js/js_function_call.asp 02/11/2024, 10 57 AM
Page 1 of 9
:
lastName: "Doe",
fullName: function () {
return this.firstName + " " + this.lastName;
}
}

// This will return "John Doe":


person.fullName();

Try it Yourself »

In the example above, this refers to the person object.

this.firstName means the firstName property of this.

Same as:

this.firstName means the firstName property of person.

What is this?
In JavaScript, the this keyword refers to an object.

The this keyword refers to different objects depending on how it is


used:

In an object method, this refers to the object.

Alone, this refers to the global object.

In a function, this refers to the global object.

In a function, in strict mode, this is undefined .

In an event, this refers to the element that received the event.

Methods like call() , apply() , and bind() can refer this to any

https://www.w3schools.com/js/js_function_call.asp 02/11/2024, 10 57 AM
Page 2 of 9
:
object.

Note
this is not a variable. It is a keyword. You cannot change the value of
this .

See Also:
The JavaScript this Tutorial

ADVERTISEMENT

The JavaScript call() Method


The call() method is a predefined JavaScript method.

It can be used to invoke (call) a method with an owner object as an


argument (parameter).

https://www.w3schools.com/js/js_function_call.asp 02/11/2024, 10 57 AM
Page 3 of 9
:
With call() , an object can use a method belonging to another object.

This example calls the fullName method of person, using it on person1:

Example

const person = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
const person1 = {
firstName:"John",
lastName: "Doe"
}
const person2 = {
firstName:"Mary",
lastName: "Doe"
}

// This will return "John Doe":


person.fullName.call(person1);

Try it Yourself »

This example calls the fullName method of person, using it on person2:

Example

const person = {
fullName: function() {
return this.firstName + " " + this.lastName;
}

https://www.w3schools.com/js/js_function_call.asp 02/11/2024, 10 57 AM
Page 4 of 9
:
}
const person1 = {
firstName:"John",
lastName: "Doe"
}
const person2 = {
firstName:"Mary",
lastName: "Doe"
}

// This will return "Mary Doe"


person.fullName.call(person2);

Try it Yourself »

The call() Method with Arguments


The call() method can accept arguments:

Example

const person = {
fullName: function(city, country) {
return this.firstName + " " + this.lastName + "," + city
+ "," + country;
}
}

const person1 = {
firstName:"John",
lastName: "Doe"
}

person.fullName.call(person1, "Oslo", "Norway");

https://www.w3schools.com/js/js_function_call.asp 02/11/2024, 10 57 AM
Page 5 of 9
:
Try it Yourself »

‹ Previous Next ›

W3schools Pathfinder
Track your progress - it's free! Sign Up Log in

ADVERTISEMENT

COLOR PICKER


https://www.w3schools.com/js/js_function_call.asp 02/11/2024, 10 57 AM
Page 6 of 9
:


ADVERTISEMENT

 PLUS SPACES GET CERTIFIED

FOR TEACHERS FOR BUSINESS

CONTACT US

Top Tutorials

https://www.w3schools.com/js/js_function_call.asp 02/11/2024, 10 57 AM
Page 7 of 9
:
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be
simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid
errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and
accepted our terms of use, cookie and privacy policy.

https://www.w3schools.com/js/js_function_call.asp 02/11/2024, 10 57 AM
Page 8 of 9
:
Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools
is Powered by W3.CSS.

https://www.w3schools.com/js/js_function_call.asp 02/11/2024, 10 57 AM
Page 9 of 9
:

You might also like