views:

31

answers:

3
function createXMLHTTP() {
           xmlhttp = =new XMLHttpRequest();
           return xmlhttp;
}

I'm trying to create 3 instances of this, but it all has the same variable name which is xmlhttp. How can I dynamically create different variable names? I'm not sure if that's the right way to ask the question.

I want to create like xmlhttp1, xmlhttp2, xmlhttp3, so then I can reference each one.

+1  A: 

How about

function createXMLHTTP() {
       var xmlhttp = new XMLHttpRequest();
       return xmlhttp;
}

xmlhttp1 = createXMLHTTP();
xmlhttp2 = createXMLHTTP();
xmlhttp3 = createXMLHTTP();

I hope this will help you

Jerome Wagner

Jerome WAGNER
+2  A: 

An easy way to create many elements is to place them in an array:

var xmlhttprequests = [];
for(var i=0;i<100;i++){
   var xmlhttp = new XMLHttpRequest();
   xmlhttprequests.push(xmlhttp);
}
Kobi
+1  A: 

You don't even need a function call for an operation this simple.

x1 = new XMLHttpRequest();
x2 = new XMLHttpRequest();
x3 = new XMLHttpRequest();

But if you insist, then at least make it shorter.

function createXHR() {
    return new XMLHttpRequest();
}
bobthabuilda