Ver Mensaje Individual
  #2 (permalink)  
Antiguo 05/01/2012, 06:07
Avatar de Dnielf
Dnielf
 
Fecha de Ingreso: diciembre-2008
Ubicación: 127.0.0.1
Mensajes: 72
Antigüedad: 15 años, 4 meses
Puntos: 14
Respuesta: Contexto de variable dentro de callback

Con this te estás refiriendo al scope creado por el callback, por lo que es mejor crear una variable, el callback buscará self en su scope y como no lo encontrará ascenderá al scope padre creado por la función mostrarAlert donde si existe self.

Código Javascript:
Ver original
  1. var MiClase = function () {
  2.     this.mensajeParaAlert = "Hola";
  3. };
  4.  
  5. MiClase.prototype.mostrarAlert = function () {
  6.     var self = this;
  7.      $(".nodos").each(
  8.      function(index,element) {
  9.           alert(self.mensajeParaAlert);
  10.      });
  11. };