dennismonsewicz
dennismonsewicz

Reputation: 25552

Backbone.js RequireJS Mixin

I have create a mixin component using requirejs like so:

define(function() {
    'use strict';

    var MyClass = function () {
        console.log('Hello World');
    };

    return {
        doSomething: function () {
            var m = new MyClass();
            return m;
        }
    }
});

My question is that is it better to write this mixin where the return is a function that can be included without accessing the returned object?

So the above would be rewritten like:

define(function() {
    'use strict';

    var MyClass = function () {
        console.log('Hello World');
    };

    return function doSomething() {
        var m = new MyClass();
        return m;
    }
});

And then when you include this module, you can then just call doSomething() versus having to include the module and access the method like myModule.doSomething().

I know that either way is acceptable, but just wanted to get an opinion on implementation.

Upvotes: 0

Views: 51

Answers (1)

Ivan Sivak
Ivan Sivak

Reputation: 7498

I think it depends on agreed conventions in your project as well as on code structure. The second example probably seems more SOLID to me since every module or class should be just single responsibility. Retrurning object notation could tend to more functions on the module returning level. Just a thought though.

Upvotes: 1

Related Questions