Ver Mensaje Individual
  #2 (permalink)  
Antiguo 12/04/2011, 15:25
Avatar de laratik
laratik
 
Fecha de Ingreso: mayo-2010
Ubicación: Cali
Mensajes: 317
Antigüedad: 13 años, 11 meses
Puntos: 63
Respuesta: asignacion de atributos en javascript

Extraño... a mi en los dos alert me aparece 123:

Código Javascript:
Ver original
  1. <script type="text/javascript">
  2. var parseData = function(){
  3.     this.xml;
  4.      
  5.     this.setXML = function(xmldom){
  6.         this.xml = xmldom;
  7.         alert(this.xml); // aqui devuelve el objeto xml normal
  8.     }
  9.      
  10.     this.getXML = function(){
  11.         alert(this.xml); // aqui es undefined :/
  12.         return this.xml;
  13.     }
  14.      
  15.     this.parseXML = function(){
  16.         alert(this.getXML);  // aqui es undefined tambien :/
  17.         $(this.xml).find("Tutorial").each(function(){
  18.             $("#output").append($(this).attr("author") + "<br />");
  19.         });
  20.     }
  21. }
  22.  
  23. function cargar() {
  24.     var obj = new parseData();
  25.     obj.setXML("123");
  26.     obj.getXML();
  27. }
  28.  
  29. window.onload = cargar;
  30. </script>

Ya que estas trabajando con pseudoclases (la POO en javascript no existe, es una ilución ) deberías adjudicarle los métodos al prototype de la clase, de esta manera (según tengo entendido) se maneja de una manera más optima la memoria, al no tener que crear los mismos métodos cada vez que instancias un nuevo objeto.

Código Javascript:
Ver original
  1. <script type="text/javascript">
  2. var parseData = function(){
  3.     this.xml;  
  4.      
  5.     this.parseXML = function(){
  6.         alert(this.getXML);  // aqui es undefined tambien :/
  7.         $(this.xml).find("Tutorial").each(function(){
  8.             $("#output").append($(this).attr("author") + "<br />");
  9.         });
  10.     }
  11. }
  12.  
  13. parseData.prototype.setXML = function(xmldom){
  14.         this.xml = xmldom;
  15.         alert(this.xml); // aqui devuelve el objeto xml normal
  16.     }
  17.  
  18. parseData.prototype.getXML = function(){
  19.         alert(this.xml); // aqui es undefined :/
  20.         return this.xml;
  21.     }
  22.  
  23. function cargar() {
  24.     var obj = new parseData();
  25.     obj.setXML("123");
  26.     obj.getXML();
  27. }
  28.  
  29. window.onload = cargar;
  30. </script>

Si entiendo bien lo ultimo que leí de tu mensaje, lo que quieres es no tener que utilizar setXML() cada vez que creas un objeto. Pues asignalo directamente como "constructor":

Código Javascript:
Ver original
  1. <script type="text/javascript">
  2. var parseData = function(xmldom){
  3.     this.xml = xmldom;
  4.      
  5.     this.setXML = function(xmldom){
  6.         this.xml = xmldom;
  7.         alert(this.xml); // aqui devuelve el objeto xml normal
  8.     }
  9.      
  10.     this.getXML = function(){
  11.         alert(this.xml); // aqui es undefined :/
  12.         return this.xml;
  13.     }
  14.      
  15.     this.parseXML = function(){
  16.         alert(this.getXML);  // aqui es undefined tambien :/
  17.         $(this.xml).find("Tutorial").each(function(){
  18.             $("#output").append($(this).attr("author") + "<br />");
  19.         });
  20.     }
  21. }
  22.  
  23. function cargar() {
  24.     var obj = new parseData("345");
  25.     obj.getXML();
  26.     obj.setXML("123");
  27.     obj.getXML();
  28. }
  29.  
  30. window.onload = cargar;
  31. </script>
__________________
Programar apasiona y lo que apasiona es un arte, por lo tanto programar es un arte.

Quiero karma para en mi próxima vida ser un billonario bien dotado con alas.

Última edición por laratik; 12/04/2011 a las 15:50