| |
Javascript "this" keyword
count the character and create a histogram
Javascript "this" keyword
"this" is not read from the scope chain as other variables are, but instead is reset on a context by context basis. Four scenarios deserve special attention:
* Calling an Object’s Method In typical object-oriented programming, we need a way of identifying and referring to the object that we’re currently working with. "this" keyword serves the purpose admirably, providing our objects the ability to examine themselves, and point at their own properties.
* Constructor - the new keyword Likewise, when defining a function to be used as a constructor with the new keyword, "this" keyword can be used to refer to the object being created.
* Function Call "this" keyword defaults to reference the most global thing it can: the window object.
* Event Handler For a more complicated twist on the normal function call, let’s say that we’re using a function to handle an onclick event. What does this mean when the event triggers our function’s execution? Unfortunately, there’s not a simple answer to this question.
|
|