Ver Mensaje Individual
  #8 (permalink)  
Antiguo 19/04/2010, 19:39
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: Objetos en javascript

GagleKas no fue mi intención criticar sino alertarte sobre tu error de concepto y, al mismo tiempo, darte pistas de dónde buscar. Pero bueno, te dejo un ejemplo donde están bien diferenciados los métodos públicos y privados, usando una función que retorna un objeto que contiene los métodos públicos:
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 
panino=(function(){
/* ---- métodos privados ---- */
    
var metodosPrivados={
        
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;
            }
            var 
ev={_obj:this,_evType:type,_fn:fn};
            
window.EvRegister=window.EvRegister || [];
            
window.EvRegister.push(ev);
            return 
this;
        },
        
removeEvent: function(typefn ) {
            if( 
this.removeEventListener){
                
this.removeEventListenertypefnfalse );
            }
            else if(
this.detachEvent){
                
this.detachEvent('on'+type,this[fn.toString()+type]);
                
this[fn.toString()+type]=null;
            }
            else{
                  
this['on'+type]=function(){};
            }
            for (var 
ii0l=window.EvRegister.lengthii ii++) {
                if (
window.EvRegister[ii]._evType== type && window.EvRegister[ii]._obj==this && window.EvRegister[ii]._fn==fn) {
                    
window.EvRegister.splice(ii1);
                    break;
                    
                }
            } 
            return 
this;
            },
         
css:function(propiedad,valor){
             if(!
valor)
                return 
this.style[propiedad];
            
this.style[propiedad]=valor;
            return 
this;
         },
         
hover:function(a,b){
             
this.addEvent('mouseover');
            
this.addEvent('mouseout');
            return 
this;
         },
         
alfa:function(value){
            
this.style.opacity value;
            
this.style.MozOpacity value;
            
this.style.KhtmlOpacity value;
            
this.style.filter 'alpha(opacity=' value*100 ')';
            
this.style.zoom=1;
            return 
this;
        },
        
toggle:function(a,b){
            
this.style.display=this.style.display=='none'?'block':'none';
            if(!!
&& !!b)
                
a.parentNode.replaceChild(b,a);
            return 
this;
        },
        
extendido:true
         
    
}
/* ---- métodos públicos ---- */
    
return{
        
extend:function(el,obj){
            if(
el.extendido && el!=metodosPrivados)return el;
            for(var 
i in obj)
                
el[i]=obj[i];
            return 
el;
        },
        
get:function(id){
            if(!
document.getElementById(id))return false;
            return 
panino.extend(document.getElementById(id),metodosPrivados);
        },
        
getO:function(obj){
            return 
panino.extend(obj,metodosPrivados);
        },
        
add:function(obj){
            
panino.extend(metodosPrivados,obj);
        },
                
        
unregisterAllEvents:function(){
                        if(
window.EvRegister)
            while (
window.EvRegister.length 0) {
                   
panino.getO(window.EvRegister[0]._obj).removeEvent(window.EvRegister[0]._evType,window.EvRegister[0]._fn);
            }
            
window.EvRegister=null;
            for(var 
i=0;el=document.getElementsByTagName('*')[i];i++)
                if(
el.extendido)
                    for(var 
ii in metodosPrivados)
                        
el[ii]=null;
            
panino=null;
        }
    }    
})();
var $=
panino.get;
panino.getO(window).addEvent('unload',panino.unregisterAllEvents);
/*----ejemplo de uso--------*/
function a(){
    var 
_this=this;
    var 
p=0,alfa=alfa || .5;
    var 
int=setInterval(
            function(){
                
p+=(500-p)/10;
                
alfa+=.015;
                if(
p>=490){
                    
clearInterval(int);    
                }
                
_this.css('width',p+'px').css('height',p+'px').alfa(alfa>=1?1:alfa);
            },
13);
}
onload=function(){
    $(
'pp').css('backgroundColor','red')
    .
css('width','50px')
    .
css('height','50px')
    .
css('cursor','pointer')
    .
addEvent('click',a)
    .
alfa(.5);
}
</script>
</head>

<body>
<div  id="pp"></div>
</body>
</html> 

Última edición por Panino5001; 19/04/2010 a las 19:45