SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

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

    Code:
      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

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    1.
    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.

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

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

    and leave out the event entirely.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •