Javascript syntax pattern

I am new to javascript and I found that I don't know a suitable way to iterate over an instance of a single variable. I open the Xml file like this:

function testXML(){
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","../res/data.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
return xmlDoc;
}

      

I added a return to select that xml file and do some searches to load some lists with data. The problem is that every time I want to take an xml file to read some data, I call this method, which not only returns the xml to me, but also does IF / Else and openfile, etc. And so on .... which I think it doesn't fit.

So how can I make a method that just returns the xml file to me, so I can only open it? Also, is it possible to immediately open the xml file and load it into a variable, say in index.html, and then navigate to other htmls without losing that variable value (xml file)?

Thank!!

+3


source to share


2 answers


var xml;

function testXML(){
    if(!xml){
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        } else {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.open("GET","../res/data.xml",false);
        xmlhttp.send();
        xml = xmlhttp.responseXML;
    }
    // return xml; // You can optionally also return the xml, but it'd be assigned to the variable already, any way.
}

      

Call this function once and you can just use the variable xml

.



Or, as shown below, just use testXML()

instead of a variable in your code and don't comment out the return.
This will ensure you don't run into undefined xml.

+5


source


I would suggest you use function closures in JavaScript to do this:

function createSingleton(fn) {
  var singleton = fn.apply(this);
  return function () {
    return singleton;
  };
}

      

It is a generic function that creates a singleton from any other function that returns something. The result will be stored inside the function's closure, and you can execute the resulting function any number of times you want to hit the singleton. If you're worried about passing this function around, you can simply store it in a global object:

var xml = createSingleton(getXML);

console.log(xml()); //use the xml object
console.log(xml() === xml());

      



What actually happens here is that createSingleton, once executed, runs the underlying factory method once, stores the result, and returns a function that returns that result. You can even do lazy loading:

function createSingletonLayz(fn) {
  var singleton = null,
      context = this;
  return function () {
    if (!singleton) {
       singleton = fn.apply(context)
    }
    return singleton;
  };
}

      

PS: Don't worry about multithreading with the usual stuff lock

to make singleton code thread safe. JavaScript only executes the code in one loop (that's why we have callbacks all over the place)

+2


source







All Articles