Javascript Reference
Categories: Operators

javascript Operators this

@March 15, 2008, 1:22 a.m.
this Firefox/Netscape/NN 2 IE 3 ECMA 1  


Refers to the current object. For example, in a form control object event handler, you can pass the object as a parameter to the function:

<input type="text" name="ZIP" onchange="validate(this);>"

Inside a custom object constructor, the keyword refers to the object itself, allowing you to assign values to its properties (even creating the properties at the same time):

function CD(label, num, artist) {
    this.label = label;
    this.num = num;
    this.artist = artist;

Inside a function, the this keyword refers to the function object. However, if the function is assigned as a method of a custom object constructor, this refers to the instance of the object in whose context the function executes.

<input type="text" name="phone" onchange="validate(this.value);">

Powered by Linode.