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(1)
<script> var myObject = { firstName:"John", lastName: "Doe", fullName: function() { return this.firstName + " " + this.lastName; } } x = myObject.fullName(); document.getElementById("demo").innerHTML = x; </script>
The this Keyword
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 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).
With call()
, an object can use a method belonging to another object.
This example calls the fullName method of person, using it on person1:
<script> var person = { fullName: function() { return this.firstName + " " + this.lastName; } } var person1 = { firstName:"John", lastName: "Doe" } var person2 = { firstName:"Mary", lastName: "Doe" } x = person.fullName.call(person1); document.getElementById("demo").innerHTML = x; </script>
This example calls the fullName method of person, using it on person2:
<script> var person = { fullName: function() { return this.firstName + " " + this.lastName; } } var person1 = { firstName:"John", lastName: "Doe" } var person2 = { firstName:"Mary", lastName: "Doe" } x = person.fullName.call(person2); document.getElementById("demo").innerHTML = x; </script>
The call() Method with Arguments
The call()
method can accept arguments:
<script> var person = { fullName: function(city, country) { return this.firstName + " " + this.lastName + "," + city + "," + country; } } var person1 = { firstName:"John", lastName: "Doe" } var person2 = { firstName:"Mary", lastName: "Doe" } var x = person.fullName.call(person1, "Oslo", "Norway"); document.getElementById("demo").innerHTML = x; </script>
Complete Code For Calling Functions In Java Script
<!DOCTYPE html> <html> <head> <title>How To Call Functions In Java Script With Examples</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> </head> <body> <div class="container"> <br> <div class="text-center"> <h1 id="color" style="color: tomato">How To Call Functions In Java Script</h1> </div> <div class="well"> <h2 id="demo"></h2> <h2 id="demo1"></h2> <h2 id="demo2"></h2> <script> var myObject = { firstName:"John", lastName: "Doe", fullName: function() { return this.firstName + " " + this.lastName; } } x = myObject.fullName(); document.getElementById("demo").innerHTML = x; var person = { fullName: function() { return this.firstName + " " + this.lastName; } } var person1 = { firstName:"John", lastName: "Doe" } var person2 = { firstName:"Mary", lastName: "Doe" } var x = person.fullName.call(person2); document.getElementById("demo1").innerHTML = x; var x2 = person.fullName.call(person1, "Oslo", "Norway"); document.getElementById("demo2").innerHTML = x2; </script> </div> </div> </body> </html>