JosephGarrone
JosephGarrone

Reputation: 4161

Objects within Objects in JavaScript

I am trying to setup a constants file for one of my sites. I am trying to set it up so I can get string constants when needed.

What I have so far (Not working, kind of C# ish)

var Constants = new SiteConstants();

function SiteConstants()
{
    this.Training = function Training()
    {
        this.Staff = function Staff()
        {
            this.fetch = "someurl";
        }
    }
}

console.log(Constants.Training().Staff().fetch); //Fail - what i want to call
//or something similar like Constants.Training.Staff.fetch
console.log((new Constants.Staff()).fetch); //Success

Is this possible in JavaScript, and if not, can someone suggest an alternative?

Upvotes: 1

Views: 137

Answers (5)

Kyle Emmanuel
Kyle Emmanuel

Reputation: 2221

Try to define each constructor properly.

function SiteConstants()
{
  this.training = new Training();
}

function Training()
{
  this.staff = new Staff();
}

function Staff()
{
  this.fetch = "someurl";
}

Or you can also do it like this:

function SiteConstants()
{
    function Training()
    {
        function Staff()
        {
            this.fetch = "someurl";
        }

        this.Staff = new Staff();
    }

    this.Training = new Training();
}

Upvotes: 2

royhowie
royhowie

Reputation: 11171

I would suggest an entirely different approach; one that is more idiomatic to JavaScript: JSON.

{
    "siteConstants" : {
        "training" : {
            "staff" : {
                "fetch" : "someurl"
            }
        } 
    }
}

Then, if you have this object attached to var constantsObject, you can access the constants as such:

constantsObject.siteConstants.training.staff.fetch;    // returns "someurl"
constantsObject["siteConstants"]["training"]["staff"]["fetch"] // returns "someurl"

Upvotes: 2

Khalid
Khalid

Reputation: 4808

you need to create a new object like this

var Obj = new Constants.Training();

then you need to call the function Staff and console.log the Obj.fetch

Obj.Staff();
console.log(Obj.fetch);

or you can do this

function SiteConstants()
{
    this.Training = function Training()
    {
        this.Staff = function Staff()
        {
            this.fetch = "someurl";
        }
    }
}
var Constants = new SiteConstants();
Constants.Training = new Constants.Training();
Constants.Training.Staff();
console.log(Constants.Training.fetch);

Upvotes: 1

Anton L
Anton L

Reputation: 422

You have to use objects instead of functions for this purpose.

var Constants = {
    Training: {
        Staff: {
            fetch: "someurl"
        }
    }
};

Upvotes: 4

ioseph
ioseph

Reputation: 1917

You are defining Training as a constructor but never actually invoking it. What you want is:

function SiteConstants()
{
    var Training = function Training() //constructor
    {
        this.Staff = function Staff()
        {
            this.fetch = "someurl";
        }
    }
    this.Training = new Training();
}

And so on for the inner class.

Upvotes: 1

Related Questions