Access and modify variable in anonymous function a

2019-09-08 16:36发布

问题:

I have an issue to access at some variables outside an anonymous function called in another function as parameter. This is the code :

clicked(){
    var lat;
    var lng;
    this.map.on('click',function(e) {
            var coordonneesDiv = document.getElementById("coordonnees");
            lat = e.latlng.lat;
            lng = e.latlng.lng;
            //var zoom = 18; 
            coordonneesDiv.innerHTML = "lat : " + lat + "<br/>" + "long : "+ lng;
            //Zoom lors d'un  click 
            //
            //this.map.setView([lat, lng], zoom);
    });
    this.getData(lat,lng); 
}

Is it possible to modify the lat and lng variables inside the anonymous function to use them after (in getData).

Thanks for you answers

-------- Correction -----

clicked(){
    var lat;
    var lng;
    var self = this;
    console.log(self);
    this.map.on('click',function(e,self){
            console.log(self);
            var coordonneesDiv = document.getElementById("coordonnees");
            lat = e.latlng.lat;
            lng = e.latlng.lng;
            //var zoom = 18; 
            coordonneesDiv.innerHTML = "lat : " + lat + "<br/>" + "long : "+ lng;
            //Zoom lors d'un  click 
            self.getData(lat,lng);
            //this.map.setView([lat, lng], zoom);
    });
    this.getData(lat,lng); 
}

Am I doing it wrong ?

The first log send me that the self exist. But the second not.

Sorry for my clumsiness...