En realidad, siguiendo las enseñanzas de nuestro amigo maborak, hay una manera más elegante de resolver las closures, con la ventaja adicional de que puede usarse más tarde para eliminar algún patrón de memory leak en explorer:
Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>test closures</title>
<html>
<head>
<script>
//credits:http://laurens.vd.oever.nl/
Function.prototype.closure = function(obj)
{
// Init object storage.
if (!window.__objs)
{
window.__objs = [];
window.__funs = [];
}
// For symmetry and clarity.
var fun = this;
// Make sure the object has an id and is stored in the object store.
var objId = obj.__objId;
if (!objId)
__objs[objId = obj.__objId = __objs.length] = obj;
// Make sure the function has an id and is stored in the function store.
var funId = fun.__funId;
if (!funId)
__funs[funId = fun.__funId = __funs.length] = fun;
// Init closure storage.
if (!obj.__closures)
obj.__closures = [];
// See if we previously created a closure for this object/function pair.
var closure = obj.__closures[funId];
if (closure)
return closure;
// Clear references to keep them out of the closure scope.
obj = null;
fun = null;
// Create the closure, store in cache and return result.
return __objs[objId].__closures[funId] = function ()
{
return __funs[funId].apply(__objs[objId], arguments);
};
};
function Ajax()
{
//--------------------------
// Variables
//--------------------------
this.handler = false; //Objeto
//--------------------------
// Funciones
//--------------------------
this.conectar = function()
{
if(navigator.appName == "Microsoft Internet Explorer") {
try {
this.handler = new ActiveXObject('Msxml2.XMLHTTP');
} catch(e) {
try {
this.handler = new ActiveXObject('Microsoft.XMLHTTP');
} catch(e) {}
}
} else {
this.handler = new XMLHttpRequest();
}
}
this.estados = function()
{
if(this.handler.readyState == 1) {
document.getElementById('estado').innerHTML = "Cargando...";
} else if (this.handler.readyState == 4) {
document.getElementById('estado').innerHTML = "Finalizado...";
document.getElementById('carga').innerHTML = this.handler.responseText;
}
}
this.enviar = function(url, metodo, valores)
{
this.handler.open(metodo, url, true);
this.handler.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
this.handler.onreadystatechange = this.estados.closure(this);
if(metodo.toUpperCase() == 'POST') {
this.handler.send(valores);
} else {
this.handler.send(null);
}
}
}
window.onload = function()
{
pagina = new Ajax();
pagina.conectar();
pagina.enviar('prueba.php', 'GET');
}
</script>
</head>
<body>
<div id="estado">En espera</div>
<div id="carga">Vacio</div>
</body>
</html>