John Smith
John Smith

Reputation: 6197

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.c.d(); too. But what if I want something like MODULESYSTEM.modules.c(); ? It should return 'modules.c'

Upvotes: 0

Views: 68

Answers (1)

Nick Husher
Nick Husher

Reputation: 1884

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.

Upvotes: 3

Related Questions