HeyDaddy
HeyDaddy

Reputation: 281

Arguments with Browserify modules

I use modules with Browserify like this :

module.exports = (function() {

'use strict';

    var hide = function(elementId) {

        var element = document.getElementById(elementId);
        element.style.opacity = 0;

    };

    return hide;

}());

As i use 'document' in my module, should I go into argument like this ?

Idem for document, window, navigator, XMLHttpRequest

module.exports = (function(document) {

    ...

}(document));

Whatever the answer, can you explain why?

Upvotes: 1

Views: 647

Answers (2)

HeyDaddy
HeyDaddy

Reputation: 281

So I write this directly :

module.exports = function(elementId) {

    'use strict';

    var element = document.getElementById(elementId);
    element.style.opacity = 0;

}

And if I want to return an object with many public methods (revealing module pattern), I have to proceed this way ? And I have to pass document?

module.exports = (function() {

    var on = function() {

    };

    var off = function() {

    };

    return {
        on: on,
        off: off
    };

})();

Upvotes: 1

mantoni
mantoni

Reputation: 1622

Browserify wraps each module in a function for you. There is no advantage in the extra wrapper. Passing document or whatever global as an argument does not win anything either. You can write browser modules as if they where node modules. Just think of each file as one large function that gets invoked on the first require.

Upvotes: 2

Related Questions