鍍金池/ 問答/HTML/ js不同回調(diào)設(shè)置變量報(bào)錯(cuò)

js不同回調(diào)設(shè)置變量報(bào)錯(cuò)

<button @click="getNearby" class="nearby">{{ destinationAddress }}</button>
    setDestinationAddress (addressComponents) {
      this.destinationAddress = addressComponents.city + addressComponents.district + addressComponents.street + addressComponents.streetNumber
    },
    getLocation (point) {
      let that = this
      var geoc = new window.BMap.Geocoder()
      geoc.getLocation(point, function (rs) {
        consoles(rs)
        that.setPoint(that.marker, rs.point)
        that.setPoint(that.destination, rs.point)
        that.setDestinationAddress(rs.addressComponents)
      })
    },
    locationSuccess ({point, addressComponent, marker}) {
      consoles(addressComponent)
      this.zoom = 16
      this.setPoint(this.current, point)
      this.setPoint(this.marker, point)
      this.setPoint(this.destination, point)
      this.setDestinationAddress(addressComponent)
    }

locationSuccess是百度地圖定位成功回調(diào),變量destinationAddress被getLocation的回調(diào)修改,再被locationSuccess修改就報(bào)錯(cuò)。

Uncaught TypeError: Cannot read property 'Ha' of null
    at Ka.eval (eval at zZ (getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1), <anonymous>:1:556)
    at Ka.x.lang.Ca.dispatchEvent (getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1)
    at Ka.Dd (getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1)
    at Geolocation.eval (eval at zZ (getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1), <anonymous>:1:1202)
    at d (eval at zZ (getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1), <anonymous>:1:1803)
    at eval (eval at zZ (getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1), <anonymous>:1:2145)
    at window.(/anonymous function) (eval at zZ (https://api.map.baidu.com/getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1:54250), <anonymous>:1:9263)
    at ip?qt=loc&coor=bd09ll&ak=aaaaa&timeout=10000&callback=_cbk16835:1
(anonymous) @ VM151757:1
x.lang.Ca.dispatchEvent @ getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1
Dd @ getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1
(anonymous) @ VM151757:1
d @ VM151752:1
(anonymous) @ VM151752:1
window.(anonymous function) @ VM151752:1
(anonymous) @ ip?qt=loc&coor=bd09ll&ak=aaaaa&timeout=10000&callback=_cbk16835:1

locationSuccess是百度地圖定位成功回調(diào),變量destinationAddress被getLocation的回調(diào)修改,再被locationSuccess修改就報(bào)錯(cuò)。不知道什么原因

回答
編輯回答
青黛色

這就屬于 js 的閉包了, 這個(gè)是在詞法作用域決定的那些變量,函數(shù)之后可以拿到~
想你定義的兩個(gè)函數(shù), 一個(gè)函數(shù)調(diào)用另一個(gè)函數(shù), 兩個(gè)函數(shù)有自己的函數(shù)作用域, 是不能拿到對(duì)方的變量的

2017年11月8日 16:12