JavaScript and I believe all other OO languages have a special keyword, this
, that you can use within a method to refer to the current object. For example, suppose you have a function called validate that validates an object's value property, given the object and the high and low values
function validate(obj, lowval, hival) {
if ((obj.value < lowval) || (obj.value > hival))
alert("Invalid Value!");
}
Then, you could call validate in each form element's onchange event handler, using this to pass it the element, as in the following example:
<input type="text" name="age" size="3"
onChange="validate(this, 18, 99)">
In general, this
refers to the calling object in a method.
I understand all of this
usage, I just have a little question: how this
works under the hood? I mean how does the method know which object is calling if you dont specify the name?