call()  Function  instance method JS Home  <<  JS Reference  <<  Function  <<  call()

Description

Calls a function with a given this value and arguments provided individually.

Syntax

Signature Description
aFunction.call(thisArg[, arg1[, arg2[, ...argN]]])Calls a function with a given this value and arguments provided individually.

Parameters

Parameter Description
thisArgThe value of this provided for the call to the function.
arg1,arg2,...argNThe arguments with which the function should be called,null or undefined if no arguments required.

Examples

Below is an example of using the call() method for chaining.


function Country(name, capital) {
  this.name = name;
  this.capital = capital;
  return this;
}
function County(name, capital, county) {
  Country.call(this, name, capital);
  this.county = county;
}
County.prototype = new Country();
// Define custom override function for County toString().
County.prototype.toString = function CountyToString() {  
  var county = this.name + ' ' + this.capital + ' ' + this.county;  
  return county;  
}
function Town(name, capital, county, town) {
  County.call(this, name, capital, county);
  this.town = town;
}
Town.prototype = new County();
// Define custom override function for Town toString().
Town.prototype.toString = function TownToString() {  
  var town = this.name + ' ' + this.capital + ' ' + 
             this.county + ' ' + this.town;  
  return town;  
}
var essex = new County('England', 'London', 'Essex');
alert(essex.toString());
alert(essex.constructor);

var barking = new Town('England', 'London', 'Essex', 'Barking');
alert(barking.toString());
alert(barking.constructor);

Press the button below to action the above code:

Related Tutorials

JavaScript Intermediate Tutorials - Lesson 8 - Functions

JavaScript Basics

JavaScript Basics

JavaScript Intermediate

JavaScript Intermediate

JavaScript Advanced

JavaScript Advanced

JavaScript Reference

JavaScript Entities

Globals

Function

Class Properties
prototype
Instance Properties
constructor
length
Class Methods
None.
Instance Methods Getters
apply()
bind()
call()
toString()
Instance Methods Setters
None.

Statements

Operators