SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    May 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript - Shortcut Coding Technique?

    I'm pretty intermediate at Javascript and recently was editing some facebook apps and came along this bit of script that does some sort of coding shortcut (I think).

    Code JavaScript:
    function typeahead(obj, options, option_ids, doc_location) {
    ...
      // Setup the events we're listening to
      this.obj.purgeEventListeners('focus')
              .addEventListener('focus', this.onfocus.bind(this))
              .addEventListener('blur', this.onblur.bind(this))
              .addEventListener('keyup', this.onkeyup.bind(this))
              .addEventListener('keydown', this.onkeydown.bind(this))
              .addEventListener('keypress', this.onkeypress.bind(this));
    ...
    }


    Does Javascript automatically know to prepend this.obj before each addEventListener? Or is something going on here?
    Last edited by Dental Floss; Jul 11, 2008 at 11:56.

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, you're being tricked by the indention. It's all a single statement like this,
    Code:
    this.x.p().a().a().a().a().a()
    The purgeEventListeners() method and the addEventListener() method apparently return the object for which they are called, allowing for a chain of method calls.
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A with statement comes to mind.

    Not that it should be used though.
    mmj

  4. #4
    SitePoint Member
    Join Date
    May 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by M-M-J View Post
    A with statement comes to mind.

    Not that it should be used though.
    This is what I have heard from others as well. Man I hate javascript

    As for them being all one line, I thought javascript didn't require a semicolon to be a valid statement, just a new line character. Taken from the ECMA-262.pdf that defines this crap, semicolons are added when:

    1. The offending token is separated from the previous token by at least one LineTerminator.
    2. The offending token is }.
    Wouldn't this code fall under the classification of being separated by a lineterminator? Each one of those lines has a different code line number associated with it, meaning newlines between each addEventListener, unless some other voodoo is going on.

    And looking at Mozilla's addeventlistener definition, I don't see a return value: http colon slash slash developer.mozilla.org/en/docs/DOM:element.addEventListener

  5. #5
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Automatic semicolon insertion is complicated. In this case there is no offending token, so no semicolons will be inserted.
    Birnam wood is come to Dunsinane


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
  •