• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

The this Keyword

Another important JavaScript resource, now that we’re discussing functions, is the this keyword. This keyword refers to the object your code is in; and to pass that object to a function, you only have to pass the this keyword. We’ll see more about the this object later, especially when dealing with HTML controls such as buttons and text fields, but we can get a preview here.

Suppose, for example, that I want to write a function to handle button clicks as we’ve already seen in Chapter 1. In this case, however, suppose that I want to display the caption of the clicked button in an alert box, so if the button’s caption is Click Me!, I want to display You clicked the Click Me! button. I can do that if I pass this function the actual button object that was clicked, because I can use that object’s value property to get the button’s caption. Here’s the JavaScript for the function, which I’ll name describer, that will do this:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint