简体   繁体   中英

Javascript, tree structure datamodel

I have this to imitate a tree structure:

var MODULESYSTEM =
{
    modules:
    {
        a : function() { return 'modules.a'; }
        b : function() { return 'modules.b'; }
        c :
        {
            d : function() { return 'modules.c.d'; }
        }
    }
}

so MODULESYSTEM.modules.a(); is valid, so MODULESYSTEM.modules.cd(); too. But what if I want something like MODULESYSTEM.modules.c(); ? It should return 'modules.c'

You won't be able to declare that sort of data structure in one line. You will need to build it up procedurally:

var MODULESYSTEM = {
    modules: {
        // Other top-level namespace objects
        c: function() {
            return 'modules.c';
        }
    }
};

// Later:
MODULESYSTEM.modules.c.d = function() { return 'modules.c.d'; };

There might be a better solution to this problem if you could provide more background about the problem you're looking to solve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM