Search code examples
javascriptclosuresiife

Declare function twice for closure?


I've got something like this:

// This executes once when the page loads.
(function() {
    //under some conditions, it calls:
    myfunction();

    function myFunction() {  
        // defines function
    }
}());

function thisIsCalledByAnOnClick() {
    // HERE I need to call myFunction()
}

I dont want myFunction() to be called from the console, so I enclosed it inside the anonymous function. So, If I need to call it somewhere else, do I declare it twice or what do I do?


Solution

    1. Define thisIsCalledByAnOnclick() within the anonymous function.
    2. Assign the onClick handle with addEventListener.