0

I have a module like this :

var myModule= {};

(function( exports ){

    init();

    exports.doSomething= function(){
        return "test";
    };

    function init(){
        initSubscribers();
        initListeners();

        initForAdmins();//just for the admin
    }

    //... a lot of functions that maybe doing things depends on if user is admin or normal user

})( myModule );

I have 2 web pages : "Normal page" use "myModule" for normal user and "Admin page" use "myModule" for admin.

Some parts of the module is used by both (normal and admin user) but some parts are specific to the type of user.

I would like to create 2 modules (myNormalModule and myAdminModule) that inherits from a general module (with shared function).

It looks strange to me to "inherit from a module".

My generalModule must be an object or it can be a module ?

3
  • A "module" in Javascript is just an object/pattern that follows certain rules. Commented Sep 27, 2015 at 1:48
  • Ok but if 5 modules inherit at the same time from 1 module, there is no context for each modules. They share the same context so they access the same data. Its why in my mind inherit a module was not possible. Commented Sep 27, 2015 at 1:56
  • I agree with you and It's what I have done. But in my case its like have a module "Shop" to buy some product (so the module use different objects inside). But now if you want to create new modules (ex: ShopForWowen and ShopForMen) that use the "Shop" module, am I suppose to reimplement Shop module into Shop object to be inherit ? Commented Sep 27, 2015 at 2:08

1 Answer 1

2

You create object types and then inherit from them. You don't inherit from a module. One module can use another module in its own implementation, but that is not inheritance.

So, first create the object structure that you want to use (including inheritance if appropriate) and then, organize that into appropriate modules. And, in organizing your code into modules, a module could define an object type and could then export (e.g. share) that object type and then other modules could either use it or inherit from it. But, the module itself is just a code organizing structure, it isn't an inheritance structure.

So, I'd suggest you figure out your object hierarchy (including inheritance) before you worry about modules and once that is designed, you can then organize it into whatever appropriates modules. For example, if all your object types for this particular problem are pretty much always going to be used together, then they might all be put in one module together.

But, if some object types for this problem might be used by themselves, then it might make sense to break them into their own module for each of sharing only appropriate pieces.

If you have context that you want to share among modules, then there are several different ways to do that depending upon exactly what it is and how it's used. The context could belong to a base object that all other objects inherit from. Or, if the context is more global, it could be stored in one module and then exported to other modules that want access to it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.