为什么let点的值不被重新分配

时间:2017-02-26 20:41:16

标签: javascript angular typescript angular2-services

我有一个名为point的变量,它更新以保存某个位置的lat和lng值。但是当我在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;

}

1 个答案:

答案 0 :(得分:4)

这与let无关。

地理编码功能被称为async,所以当它第一次打印时,它实际上没有被分配。