0

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'

1 Answer 1

3

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.

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

1 Comment

I wanted to referrer a node as it were a leaf, not an "array"

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.