1

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?

Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
  • [You can expose the function outside of the closure](http://stackoverflow.com/a/13074081/1257652) to accomplish what you are needing.. – Brett Weber Jul 27 '13 at 21:07

4 Answers4

2

Within the closure thisIsCalledByAnOnClick has access to myFunction. For further information see: module pattern.

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

    function myFunction() {
        // defines function
    }

    return {
        thisIsCalledByAnOnClick : function() {
        // HERE I need to call myFunction()
        }
    };
}();
Matthias Holdorf
  • 1,040
  • 1
  • 14
  • 19
1
  1. Define thisIsCalledByAnOnclick() within the anonymous function.
  2. Assign the onClick handle with addEventListener.
Derick Leony
  • 421
  • 2
  • 7
0

You can usee a constructor function to encapsulate both functions. One as a public function and the other private.

D.

0
function() {
     xyz = function() {
           return this;
           };

     xyz.prototype= {

     Myfunction1 : function(param1,param2)  {
     //some code!!!
     },
     Myfunction2 : function(param1,param2) {
     //some code!!!
     }
     };
})();

function thisIsCalledByAnOnClick() {

        instance=new xyz();
        instance.Myfunction1(a,b)
}
HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87