Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments into the revealing Module pattern in Javascript

How can you pass arguments to a function when implementing the Revealing module pattern in JavaScript. I have a module like this

var GlobalModule = (function() {
    function consoleLog(textToOutput) {
        console.log(textToOutput);
    }

    return {
        consoleLog: consoleLog()
    };
})();

Later on, when i run GlobalModule.consoleLog("Dummy text");, I get undefined as the output.

like image 774
cr05s19xx Avatar asked Jan 23 '26 09:01

cr05s19xx


1 Answers

return {
    consoleLog: consoleLog()
};

That part of your code is wrong. You're exporting the Result of the consoleLog call due to the () at the end of the function, where you want to export the function itsself. So just remove the function call:

return {
    consoleLog: consoleLog
};
like image 62
Shilly Avatar answered Jan 25 '26 06:01

Shilly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!