Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

any implication of using a regular function instead of function expression within a js module pattern? [duplicate]

Tags:

javascript

With a js module pattern, I think functions are commonly defined like this:

var loadData = function(myParam1, myParam2){}

However, the js module pattern still works with functions defined like this:

function loadData (myParam1, myParam2){}

Is there any practical reason to define functions as variables in a js module? Is a function variable generally expected from a design standards perspective for a publicly exposed method within a js module? Or is the implementation style really more of a matter of personal preference?

like image 750
details1 Avatar asked Feb 15 '26 00:02

details1


1 Answers

Module pattern is usually augmented by IIFE pattern:

(function(){
})();

Here is an example:

var MODULE = (function(){
    function anotherLoadData(myParam1, myParam2){
        console.log('another load data')
    }
    return {
        loadData : function(myParam1, myParam2){ 
            console.log('load data');
        },
        anotherLoadData : anotherLoadData
  }
})();

MODULE.loadData();
MODULE.anotherLoadData();

So you see, the way you declared your functions doesn't relate to js module pattern.

like image 179
singsuyash Avatar answered Feb 16 '26 13:02

singsuyash



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!