JS Tutorials
JS Objects
JS Functions
JS Classes
JS Async
With the call()
method, you can write a method that can be
used on different
objects.
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.
In a function definition, this
refers to the "owner" of the
function.
In the example above, this
is the person
object that "owns" the
fullName function.
In other words, this.firstName means the firstName property of this object.
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).
With call()
, an object can use a method belonging to
another object.
This example calls the fullName method of person, using it on person1:
This example calls the fullName method of person, using it on person2:
The call()
method can accept arguments: