为什么 let point 的值没有被重新分配
Why is value of let point not being reassigned
我有一个名为 point 的变量,它会更新以保存某个位置的纬度和经度值。但是,当我在 if 函数中重新分配时,点的值不会更新。一旦进入 if 函数,point 的值就会被重新分配并打印出正确的值,但是当它离开 if 函数时,它又回到未定义状态。
generateWayPoint(address: string): string {
let point;
const point4 = new google.maps.LatLng(51.496144, -3.182328);
const geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
point = new google.maps.LatLng(results[0].geometry.location.lat(),
results[0].geometry.location.lng());
this.wayPoint = point;
console.log('a point in the making', this.wayPoint);
console.log('a wayPoint in the making', this.wayPoint);
} else {
console.log('Geocode was not successful for the followin reason:', status);
}
});
console.log('a point how it should be:', point4);
console.log('comparing a point', point);
console.log('comparing a wayPoint', this.wayPoint);
return point;
}
这与let无关
地理编码函数被称为异步,所以在它第一次打印点时,它确实没有被赋值。
我有一个名为 point 的变量,它会更新以保存某个位置的纬度和经度值。但是,当我在 if 函数中重新分配时,点的值不会更新。一旦进入 if 函数,point 的值就会被重新分配并打印出正确的值,但是当它离开 if 函数时,它又回到未定义状态。
generateWayPoint(address: string): string {
let point;
const point4 = new google.maps.LatLng(51.496144, -3.182328);
const geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
point = new google.maps.LatLng(results[0].geometry.location.lat(),
results[0].geometry.location.lng());
this.wayPoint = point;
console.log('a point in the making', this.wayPoint);
console.log('a wayPoint in the making', this.wayPoint);
} else {
console.log('Geocode was not successful for the followin reason:', status);
}
});
console.log('a point how it should be:', point4);
console.log('comparing a point', point);
console.log('comparing a wayPoint', this.wayPoint);
return point;
}
这与let无关
地理编码函数被称为异步,所以在它第一次打印点时,它确实没有被赋值。