Ver Mensaje Individual
  #4 (permalink)  
Antiguo 04/04/2009, 20:28
Avatar de Panino5001
Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 20 años
Puntos: 834
Respuesta: Como hacer esto

Creo que te referís al encadenamiento (chaining). Se logra extendiendo los objetos para agregarle funcionalidad y haciendo que cada una de estas extensiones retorne el mismo objeto. Un ejemplo reducido:
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=utf-8" />
<
title></title>
<
script>
var 
fw=(function(){
    var 
metodosPrivados={
        
css:function(prop,valor){
            if(!
valor)
                return 
this.style[prop];
            else
                 
this.style[prop]=valor;
            return 
this;
        },
        
addEvent: function(typefn ) {
            if ( 
this.addEventListener ) {
                
this.addEventListenertypefnfalse );
            } else if(
this.attachEvent){
                var 
_this=this;
                var 
f= function(){fn.call(_this,window.event);}
                
this.attachEvent'on'+typef);
                
this[fn.toString()+type]=f;
            }else{
                
this['on'+type]=fn;
            }
            return 
this;
        }
    }
    return {
        
extend:function(molde,obj){
            for(var 
i in molde)
                
obj[i]=molde[i];
            return 
obj;
        },
        
get:function(id){
            return 
fw.extend(metodosPrivados,document.getElementById(id))
        }
    }
})();
var $=
fw.get;
onload=function(){
    $(
'p').css('backgroundColor','orange').css('width','500px').addEvent('click',function(){this.css('backgroundColor','red');});
}
</script>
</head>

<body>
<div id="p" style="background-color:#F00">test</div>
</body>
</html> 
Notar el return this en cada funcionalidad agregada.
pato12, un favor: usá títulos más descriptivos así le sirven a alguien que use el buscador del foro.

Última edición por Panino5001; 04/04/2009 a las 20:48