Book: Simply Javascript 2007 - page 135 - this and event

on page 135 there is the following excerpt from tooltips.js:

  showTipListener: function(event)
  {
    Tooltips.showTip(this);
    Core.preventDefault(event);
  },

  hideTipListener: function(event)
  {
    Tooltips.hideTip(this);
  }
  1. why is showTip using “this” but preventDefault using “event” ?

  2. why is hideTip using “this” when “event” is the parameter?

i know it’s right under my nose but i’m not quite getting it

many thanks :slight_smile:

Hi there,

In showTipListener this refers to the element on the page which triggered the event, so in this case an anchor element.
Whereas event is a MouseEvent, who’s default action needs preventing.

this is available within the scope of the function, without having to pass it explicitly as a parameter.
You could also write:

hideTipListener: function()
{
  Tooltips.hideTip(this);
}

and leave out the event entirely.

ah, thanks, getting clearer, will re-read this section but much better

this is available within the scope of the function

i remember reading about ‘this’ and scope within and outside of a function, will re-read this section of the book

thanks