Reputation:
I put all my code in NS, similar to how jQuery is structured. I have a few variables global to NS, that I would like to enclose and then make accessible like this -> Global.variable_name
.
Below is the way I did it. Is this good practice? Is there a better way to do this where I do not have to call var Global = new GlobalMaker()
I use all capitals for global CONSTANTS.
var NS = ( function ( window, undefined ) { /* all my code is here */ } )( )
/** (including this code)
*GlobalMaker
*/
var GlobalMaker = function()
{
this.tag_array = [];
this.current_tag;
this.validate_input_on;
this.JSON_ON = 1; // selector between JSON and LON
this.GATEWAY = 'class.ControlEntry.php'; // for Ajax calls
this.PICTURES = '../pictures/'; // for composing tweets
this.PASS = 0;
this.FAIL = 1;
this.NOTDEFINED = 2;
};
var Global = new GlobalMaker();
/**
*Global
*/
var Global =
{
tag_array: [],
current_tag: 0,
validate_input_on: 0,
JSON_ON: 1,
GATEWAY: 'class.ControlEntry.php',
PICTURES: '../pictures/',
PASS: 0,
FAIL: 1,
NOTDEFINED: 2
}
Upvotes: 1
Views: 888
Reputation: 50787
This does the equivalent, without using a constructor function:
var Global = {
tag_array: [],
// current_tag, // huh?
// validate_input_on, // huh?
JSON_ON: 1,
GATEWAY: 'class.ControlEntry.php',
PICTURES: '../pictures/',
PASS: 0,
FAIL: 1,
NOTDEFINED: 2
};
But I don't understand the second two declarations that have no initialization.
Note, though, that if you do this as you indicate above, this Global object is available only within that outer Arc
function expression. It's not truly global. Also note that the use of 'global
' (lowercase) is pretty common; you might want to consider a different varialbe name (perhaps 'constants
'?)
Upvotes: 2