1

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?

4
  • since they are constructors, (new (new Constants.Training()).Staff()).fetch Commented Jul 24, 2014 at 1:28
  • 1
    Constants per se do not exist in Javascript. If you are trying to create objects in other objects, just use object literal notation and assignment as appropriate. Commented Jul 24, 2014 at 1:29
  • @K.Hallman While you are right, there is the const keyword (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…) but I wouldn't suggest it since it isn't widely supported yet. And I think it's pretty obvious what the OP means by "constants", and object literals, as you suggest, are probably the best way to go... Commented Jul 24, 2014 at 1:30
  • @Ian const is a reserved word but not presently implemented in the ECMAScript spec, to my knowledge. The OP was confusing because it used a word that has a specific meaning in an unclear way. Commented Jul 30, 2014 at 18:57

5 Answers 5

4

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

var Constants = {
    Training: {
        Staff: {
            fetch: "someurl"
        }
    }
};
Sign up to request clarification or add additional context in comments.

Comments

2

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"

4 Comments

There's no "JSON" type in JavaScript. You're creating an object literal, which another answer has already provided
@Ian I never called it a type. I said "approach."
I still don't see why JSON should even be referenced here. JSON is a data transfer format, and has nothing to do with JavaScript (other than the fact that it was derived from JS objects)
@Ian Also, I hadn't seen Anton L's answer until after I had posted mine. Regardless, JSON could be used externally, which would probably serve the OP's request better: "I am trying to setup a constants file for one of my sites."
2

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();
}

1 Comment

I needed to have them enclosed, so that there would be no conflicting objects (We have a ton of javascript objects on our site)
1

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.

Comments

1

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);

Comments

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.