2010-12-03 15 views
1

portée Disons que j'ai une classe:Javascript Object Fonction

var asdf = new Class({ 
    myFunction: function() { 
    //some stuff here 
    }, 
    anotherFunction: function() { 
    globalObject.dosomethingandusecallback( 
     function() { // this is the callback 
     //how do I call myFunction() here? I can't seem to get it to work? 
     } 
    ); 
    } 
}); 

Il me semble avoir des problèmes de cadrage en essayant d'appeler maFonction dans la définition de ma fonction de rappel. Qu'est-ce que j'oublie ici? Je pensais qu'il devrait avoir accès à myFunction dans ce contexte?

Merci!

Répondre

3

Copiez le mot-clé this dans une variable en dehors de la fonction de rappel, et utiliser cette variable dans le rappel:

anotherFunction: function() { 
    var self = this; 
    globalObject.dosomethingandusecallback( 
    function() { // this is the callback 
     self.myFunction(); 
    } 
); 
}