Search code examples
javascriptjquerydesign-patternsmodule-pattern

How to call this JS function that is set to a variable in console?


I have a function like below:

$(document).ready(function() {
    var funct = (function(){
        var foo = 1;

        function getPrivateVariable(){
            return foo;
        }

        return {
            getFoo: getPrivateVariable
        };
    })();
});

Then I try to type funct.getFoo(); into the console of firebug and it says "funct is undefined". How come or how can I call this function in console. If it is not possible in this form(I am using a module design pattern), then why is it not possible?


Solution

  • You have to make sure that the function is defined on the global scope, because if it's inside a closure, the console won't see it. Instead of this:

    var funct = (function(){
    

    use

    window.funct = (function(){