JavaScript » Special » this

The keyword this is used to refer to the current object. In a method, it usually refers to the calling object.

Examples

Code:
function describeAge(obj)
{
   if(obj.year < 1996)
      return "Old-fashioned"
   else
      return "Good-as-new"
}

function car(make, year, description)
{this.make = make, this.year = year, this.description = describeAge(this)}
 
myCar = new car("Ford", "1993", describeAge(this))
Explanation:

In this example the code first creates a function DescribeAge that takes as its parameter an object, and returns one of two text values depending on the value of that same object's Year property. Then another object called Car is created whose third property Description is the value returned by the DescribeAge function. The keyword this is used as the parameter of the DescribeAge function to refer to whichever object is calling it, as seen in the final bit of code which creates a specific instance of the Car object whose Description property will now contain the string "Old-fashioned".