Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript - How to define constants/values used by multiple function

Posted on 2012-04-08
4
Medium Priority
?
320 Views
Last Modified: 2012-04-16
I understand Javascript do have the notion of constant.  However I would like to use a set of constant value across multiple functions.  How will I do that?
For example I have the following set of variables that I would like to treat as constant and used in different functions.
     var FORM_TYPE_CREATE = 1;
     var FORM_TYPE_UPDATE = 2;
     var FORM_TYPE_READ_ONLY = 3;
     var FORM_TYPE_DISABLED = 4;
     var FORM_TYPE_QUICK_CREATE = 5;
     var FORM_TYPE_BULK_EDIT = 6;
0
Comment
Question by:tommym121
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 23

Assisted Solution

by:basicinstinct
basicinstinct earned 800 total points
ID: 37822141
you have already done it - put your vars in uppercase. that's how you define a constant in javascript.
obviously it's not really a constant, the naming convention simply tells other developers that they should treat these as constants and not change the vlaues OR DIE.
0
 
LVL 23

Assisted Solution

by:basicinstinct
basicinstinct earned 800 total points
ID: 37822144
unless you are in firefox only... then you have "const"

https://developer.mozilla.org/en/JavaScript/Reference/Statements/const
0
 
LVL 6

Accepted Solution

by:
jjperezaguinaga earned 1200 total points
ID: 37822596
Greetings tommym121,

I agree with basicinstinct, it's just a naming convention. Now, you could do closures instead and wrap your constants in a function:

function CONSTANTS()
{
        constants = {};
        //List your constants
        constants.FORM_TYPE_CREATE = 1;
        ....
        return function () {
             return constants;    
        };

}

Open in new window


This way you can encapsulate your "constants" in a local function that does not clutter the global namespace, and it's available for read only purposes.

//Retrieve an specific constant during run-time
var form_type_create = CONSTANTS().FORM_TYPE_CREATE;

Open in new window


You can even go fancy and use augmented loosely pattern in order to update the values at some point

CONSTANTS = (CONSTANTS(constants_object, new_value_for_form_type_create)
{
   var old_constant = constants_object.FORM_TYPE_CREATE;
   constants_object.FORM_TYPE_CREATE = new_value_for_form_type_create || old_constant;
   //In case, for instance, no value was provided.
})(CONSTANTS(), 2)

Open in new window


Don't you just love Javascript?
Cheers!
-JJ
0
 

Author Closing Comment

by:tommym121
ID: 37852969
Thanks
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question