Skip to content
Related Articles

Related Articles

Improve Article

JavaScript | Function Call

  • Last Updated : 12 Mar, 2019

It is a predefined javascript method, which is used to write methods for different objects. It calls the method, taking the owner object as argument. The keyword this refers to the “owner” of the function or the object it belongs to.

Syntax:

call()

Return Value: It calls and returns a method with the owner object being the argument.

Example 1: It uses call() method to call the employee as argument.




<!DOCTYPE html>
<html>
      
<head>
    <title>
        JavaScript Function Call
    </title>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
      
    <h2>JavaScript Function Call</h2>
      
    <p>
        It calls the employee details of emp2
    </p>
      
    <p id="GFG"></p>
      
    <!-- Script to use call() method and display
        the emp2 details -->
    <script>
        var employee = {
            details: function() {
                return this.name + " " + this.id;
            }
        }
          
        var emp1 = {
            name:"Geeks",
            id: "234412",
        }
          
        var emp2 = {
            name:"G4G",
            id: "434556",
        }
          
        var x = employee.details.call(emp2); 
        document.getElementById("GFG").innerHTML = x; 
    </script>
</body>
  
</html>                    

Output:

Example 2: This example describes the use of function call with arguments.




<!DOCTYPE html>
<html>
  
<head>
    <title>
        The call() Method with Arguments
    </title>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
      
    <h2>JavaScript Function Call</h2>
      
    <p>
        It calls the employee details of emp2
    </p>
      
    <p id="GFG"></p>
      
    <script>
        var employee = {
            details: function(designation, experience) {
                return this.name + " " + this.id + "<br>" 
                       + designation + "<br>" + experience;
            }
        }
          
        var emp1 = {
            name:"A",
            id: "123",
        }
          
        var emp2 = {
            name:"B",
            id: "456",
        }
          
        var x = employee.details.call(emp2, "Manager", "4 years"); 
        document.getElementById("GFG").innerHTML = x; 
    </script>
</body>
  
</html>                    

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :