Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Sub modules

  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Sub modules

    Hello,
    I have a question about sub-modules.
    Code example:

    [CODE]
    /create module
    var MODULE = (function () {
    function main(){
    alert ("main");
    }
    return {
    main: main
    }
    }());


    // add a sub-module (...maybe somewhere later in programflow)
    MODULE.submodule = (function(){
    function callMain(){
    MODULE.main();
    }
    return {
    callMain: callMain
    }
    }());


    //execute:
    MODULE.submodule.callMain();

    [CODE]


    Now this code works fine. But in the submodule I don't want to use:
    [ICODE]MODULE.main();[ICODE]
    but simply:
    [ICODE]main():[ICODE]

    Does anyone have a solution for this?
    tnx in advance.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    You can't call just main() because it is a method of the MODULE var. Unless you create an alias to MODULE.main function by:

    Code:
    var main = MODULE.main;
    But I don't think that's what you are really after. Maybe this is:

    Code:
    var MODULE = (function () {
        function main(){
            alert ("main");
        }
        return {
            main:   main
        }
    }());
    
    // add a sub-module (...maybe somewhere later in programflow)
    MODULE.submodule = function(){
        var me = this;
        function callMain(){
            me.main();
        }
        return {
            callMain:   callMain
        }   
    };
    
    MODULE.submodule().callMain();
    Btw, your [code] and [icode] tags are mismatched. Please fix it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •