Should I use a global variable and if not, what instead? (Javascript) -


i'm working several functions need pass variable , forth. should use global variable or method instead? appreciate example or 2 on how implement it.

thanks, elliot bonneville

psuedocode of functions:

function getxml() {//this function reads in xml file.                   //preferably generate or set object hold xml data. }  function usexmldata() { //i use xml data here. }  function usexmldatahereaswell() { //and here well. } 

the best solution you're trying wrap data object , make functions methods on object:

function myxmlclass() {   this.data = null; }  myxmlclass.prototype = {   getxml: function() {     this.data = ...;   },    usexmldata: function() {     // use this.data   },    /* etc. */ }; 

and can use this:

var x = new myxmlclass(); x.getxml(); x.usexmldata(); ... 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -