[英]How to store data in variable that is out of scope in Javascript ES6
I am new to the ES6. 我是ES6的新手。 I am facing problem storing data in variable that is out of scope.I have created button event and associated
getLocation()
to it to find users location but I am not able to find way to update lon, lat variables which are out of scope. 我面临将数据存储在超出范围的变量中的问题。我创建了按钮事件并将其与
getLocation()
关联以查找用户位置,但是我无法找到更新超出范围的lon,lat变量的方法。 Bellow is the code : 波纹管是代码:
lon: any;
lat: any;
getLocation() {
var lat;
var lon;
navigator.geolocation.getCurrentPosition(function(d) {
lat = d.coords.latitude.toFixed(2);
lon = d.coords.longitude.toFixed(2);
console.log(this.lat);
console.log(this.lon);
});
}
Update(lat: any, lon: any) {
this.lat = lat;
this.lon = lon;
}
lon: any;
lat: any;
getLocation() {
var self = this;
navigator.geolocation.getCurrentPosition(function(d) {
self.lat = d.coords.latitude.toFixed(2);
self.lon = d.coords.longitude.toFixed(2);
console.log(self.lat);
console.log(self.lon);
});
}
You need to leave class variables as it is and u need to just update them. 您需要保持类变量不变,而您只需要更新它们即可。 when u create
var lon
and var lan
you are creating method variables and just keeping the value within the method. 当您创建
var lon
和var lan
您正在创建方法变量并将值保留在方法内。 Thus it will not update the class variable. 因此,它将不会更新类变量。
Read More: Member variables in ES6 classes 阅读更多: ES6类中的成员变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.