Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Sep 2010
    Location
    The Twilight Zone
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts

    How the heck does this function work?!

    Hey guys,

    I'm beginning to learn javascript, and I've come across functions like the one below in many scripts. I just don't understand the purpose of the parameter! Nothing is being passed into the function when it's being called- so what's the point of specifying an argument???

    Code:
    function doSomething(e) {
    	if (!e) var e = window.event;
    	alert(e.type);
    }
    Why not:

    Code:
    function doSomething() {
    	var e = window.event;
    	alert(e.type);
    }

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 592 Times in 572 Posts
    what you have is a cross-browser event handler.

    something is being passed if that function gets bound as an event.

    it's the event object that is passed. in older IEs, the same object (well about the same) is not passed, but is available at window.event.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • Users who have thanked rnd me for this post:

    Ahlahn (08-01-2011)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,947
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    In other words, that code is kind of equivalent to doing this:
    Code:
    function doSomething(e) {
    	if ( this_is_an_older_MSIE_browser ) { e = window.event; } else { /* it's not IE or it's modern IE */ }
    	alert(e.type);
    }

  • #4
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by Ahlahn View Post
    Nothing is being passed into the function when it's being called...
    Not quite correct.

    The event object is being passed to the function.

  • Users who have thanked bullant for this post:

    Ahlahn (08-01-2011)

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by bullant View Post
    The event object is being passed to the function.
    hey, that's what i said:
    Quote Originally Posted by rnd me
    it's the event object that is passed.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #6
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    and I included a link to more information.

    If you have an issue with my post, report it as fluff to the moderators. If they agree with you they have the option to delete it. If they don't, I guess it will stay

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by bullant View Post
    and I included a link to more information.

    If you have an issue with my post, report it as fluff to the moderators. If they agree with you they have the option to delete it. If they don't, I guess it will stay
    lol. if you really want to be a johnny-come-lately copycat, more thread counts to you, but some find it irksome. i guess that's how you got that many posts since feb...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • Users who have thanked rnd me for this post:

    Philip M (08-01-2011)

  • #8
    New Coder
    Join Date
    Sep 2010
    Location
    The Twilight Zone
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    I see. Since some browsers like IE passes the event object explicitly to the calling function, we need a parameter in place to check for that. Why is the DOM so complicated? Haha.

    Thank you all for you answers. Also, the link was very helpful, so thanks to Bullant for providing an extra source of information.

  • #9
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by Ahlahn View Post
    Thank you all for you answers. Also, the link was very helpful, so thanks to Bullant for providing an extra source of information.
    You're welcome


  •  

    Posting Permissions

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