...

View Full Version : Javascript Array... global or local?



LynxGrr
07-02-2004, 01:44 PM
Hello!

Just a general question this. On one of my current personal projects (a Dungeons and Dragons Charachter Generator) Im using a large array embedded within a function at present. When the function is called the array is being built each and every time its needed (or im guessing thats the case). If i were to seperate out the array building section of the code, and put it into a seperate js file which is called when the page is loaded, would this array be available to other js files? If not, how would I make it available?

Any help is appreciated.

glenngv
07-02-2004, 02:20 PM
Just declare the variable outside the function using the var keyword.

var bigArray = new Array();

var is not required because the variable will be automatically global if declared without it, but it is good practice to always use it.

LynxGrr
07-02-2004, 04:37 PM
Thank you very much.

Does this mean I can reference to the array from other functions in other JS files.

In case you hadn't notice im a newbie. Ive only ever used JS for form validation.

Roy Sinclair
07-02-2004, 07:44 PM
Making it global does indeed make it accessible to any function. However, in a game that's NOT a good idea. Anyone who can perform a script injection will therefore be able to alter any value in that array and if you're posting changes back to a server that could allow rampant cheating.

I think you may want to look into an object oriented (OO) design for both your data and the scripts that may access it. Using OO design methodologies you can carefully control the access to the array and make it much more difficult for a script injection to work.

LynxGrr
07-03-2004, 04:15 PM
Its not a game as such. Its an online charachter generator for an offline game. Ill be using things like FPDF to output a pdf file which can be printed out for example. Plus if people want to cheat then thats their own personal enjoyment taken out of the game!

However thank you for your comments though. Certainly something to bear in mind for the future



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum