The ‘this’ keyword is indispensable in JavaScript object-oriented programming. When used in a JavaScript constructor function, ‘this’ refers to the object. Through the ‘this’ keyword, properties and methods can be assigned object, also known as a class. For example:
function Square(intSideLength)
{
this.sideLength = intSideLength;
}
In the preceding example the ‘this’ keyword is used to assign the variable ‘sideLength’ as a property of the Square class.
The ‘this’ keyword is also frequently passed as a parameter on JavaScript events, such as when a checkbox is clicked. In such an instance, ‘this’ refers to the current object, the checkbox.
Filed under: programming, software |
Leave a comment