More and more javascript
has this modular approach where empty functions are returned. You can find it in the source of node.js
. It's in the documentation of require.js
. What does it do?
define(['jquery'] , function ($) {
return function() {};
});
If I would return an object, I would just do return {};
, although I see no use in returning an empty object. Might wanna put the vars in it. So there is obviously something valuable about this. I'm guessing it has something to do with accessing functions that were in the scope of the returned function.
Here is that construction again, in the AMD Is Not The Answer article:
define(['dep1', 'dep2'], function (dep1, dep2) {
//Define the module value by returning a value.
return function () {};
});
But what is the practical benefit above in stead of just returning the semantically meaninful stuff you want to return, e.g. in an object?
And how do you access outside-function-inside-scope things in this way?
function(baz) {
var foo = true,
bar = false;
if (baz) bar = true;
return function() {}; // wut?
};
Although I am not exactly technically 'challenged', I was hoping someone could explain this in layman's terms, because articles about modularity do not make me happy.