Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Array... global or local?

    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.
    "The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."

    --Tim Berners-Lee, W3C Director and Inventor of the World Wide Web

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    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.

  • #3
    New Coder
    Join Date
    May 2004
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    "The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."

    --Tim Berners-Lee, W3C Director and Inventor of the World Wide Web

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #5
    New Coder
    Join Date
    May 2004
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    "The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."

    --Tim Berners-Lee, W3C Director and Inventor of the World Wide Web


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •