3

I'm working from a JavaScript book and would like to create some menus using callback functions and some intelligent use of event handling. I have some code that looks like this:

window.onload = InitPage;

function InitPage(){

function hoverMenu(e, oTarget){
    var isChildOf = function(pNode, cNode){
        //alert("value of pNode:" + pNode + " value of cNode" + cNode);
        if(pNode === cNode){
            return true;
        }
        
        while (cNode && cNode !== pNode){
            cNode = cNode.parentNode;
        }
        
        return cNode === pNode;
    }
    
    //alert(e.srcElement);
    var target = e.target;
    
    if(!oTarget){
        oTarget = target;
    }
    
    var relTarg = e.fromElement;
    
    if(isChildOf(oTarget, relTarg) == false){
        alert("mouse enters");
    }
}

function unhoverMenu(e, oTarget){
    var isChildOf = function(pNode, cNode){
        //alert("value of pNode:" + pNode + " value of cNode" + cNode);
        if(pNode === cNode){
            return true;
        }
        
        while (cNode && cNode !== pNode){
            cNode = cNode.parentNode;
        }
        
        return cNode === pNode;
    }
    
    //alert(e.srcElement);
    var target = e.target;
    
    if(!oTarget){
        oTarget = target;
    }
    
    var relTarg = e.toElement;
    
    if(isChildOf(oTarget, relTarg) == false){
        alert("mouse leaves");
    }
}

var ul_menu = document.getElementById("ul_grabbed");
ul_menu.addEventListener("mouseover", function(e1){return function(e){hoverMenu(e, e1)}}(ul_menu),true);
ul_menu.addEventListener("mouseout", function(e1){return function(e){unhoverMenu(e, e1)}}(ul_menu),true);

I have run through the code. What essentially happens here is a triggered event when the mouse enters a UL element or it's children as well as a triggered event for leaving that UL or it's children. I'm having trouble understanding all but the last piece shown here:

ul_menu.addEventListener("mouseover", function(e1){return function(e){hoverMenu(e, e1)}}(ul_menu),true);
ul_menu.addEventListener("mouseout", function(e1){return function(e){unhoverMenu(e, e1)}}(ul_menu),true);

My understanding of these lines is that we are adding an event to the UL element with two functions, one returned from inside the first, that will fire on the capture phase of the event. My questions are why I need (ul_menu) at the end of these functions and with there being e1 and e does this mean that there are in fact two different events being triggered here? If someone could just explain those two last lines to me I would greatly appreciate it.

1
  • Is that code from a JavaScript book? I don't like it. For instance, why defining isChildOf twice, inside each event handler? And the outer functions that confused you aren't necessary at all here. Commented Apr 1, 2012 at 3:11

1 Answer 1

3

It's a self-executing function that returns a function, and it's a way of passing an extra parameter into the event handler. If you take the outer layer --

function(e1){ ... }(ul_menu)

-- then you'll see you immediately get back what's inside the { ... } container, which is this:

function(e) { hoverMenu(e, ul_menu); }

This is then what becomes the event-handler. So, e is the event, but now the extra paremeter ('target' in this case) ul_menu gets passed to your 'hoverMenu' handler.

Sign up to request clarification or add additional context in comments.

2 Comments

Ahhhhh... so if I'm to understand this, putting the following: function(e1){ ... }(ul_menu) is basically declaring the function in-line and then immediately passing it the parameter ul_menu to be run in-line as well. Correct?
@Mike yes, although looking at it a second time I think bfavaretto is right, I don't see how it's necessary to do that here. Still, it's a common design pattern in Javascript, maybe the book is trying to soften you up for it. I like this link for JS design pattern reference: addyosmani.com/resources/essentialjsdesignpatterns/book/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.