Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

this Statement (JavaScript)

Refers to the current object.

this.property

The required property argument is one of the current object's properties

The this keyword can be used in object constructors to refer to the current object.

In the following example, this refers to the newly created Car object, and assigns values to three properties:

function Car(color, make, model){
   this.color = color;
   this.make = make;
   this.model = model;
}

The this keyword generally refers to the window object if used outside of the scope of any other object. However, inside event handlers this refers to the DOM element that raised the event.

In the following code (for Internet Explorer 9 and later), the event handler prints the string version of a button that has an ID of "clicker".

document.getElementById("clicker").addEventListener("click", eventHandler, false);

        function eventHandler(ev) {
            document.write(this.toString());
        }

// Output (when you click the button): [object HTMLButtonElement]

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported in Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.