Pergunta

I have the following methods.. I'd like to be able to mock something up so I can test whether or not pete() has been called. Not sure how to do this when im using closures. Any ideas ?

bla = (function(){
     var a = 0;
     jim = function(){
         if(a==1){
             pete();
         }
     },
     pete = function(){
         return 1;
     }

     var publicInterface = {
         "publicjim": jim
     }
     return publicInterface;
})();
Foi útil?

Solução

In your self-executing anonymous function, you're using object propertys.

jim and pete need to be local variables in order to "hide" them via closure.

var bla = (function(){
    var a = 1;
    var jim = function() {
      if (a == 1) {
         pete();
      }
    };
    var pete = function() {
        return 1;
    };

    return {
       "publicjim": jim
    };  
})();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top