Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript CoffeScript绑定变量_Javascript_Variables_Binding_Coffeescript - Fatal编程技术网

Javascript CoffeScript绑定变量

Javascript CoffeScript绑定变量,javascript,variables,binding,coffeescript,Javascript,Variables,Binding,Coffeescript,我有一节课 class window.MapHandler map = null userLocationMarker = null makeMap: (location) -> myOptions = zoom: 14 center: location mapTypeId: google.maps.MapTypeId.ROADMAP @map = new google.maps.Map(document.getElemen

我有一节课

class window.MapHandler
  map = null
  userLocationMarker = null

  makeMap: (location) ->
    myOptions =
      zoom: 14
      center: location
      mapTypeId: google.maps.MapTypeId.ROADMAP
    @map = new google.maps.Map(document.getElementById("map_canvas"), myOptions)
  placeMarker: (location, icon_path) ->
    if icon_path
      markerImage = new google.maps.MarkerImage(icon_path, null, null, null, new google.maps.Size(25, 25))
    else
      markerImage = null
    marker = new google.maps.Marker(
      position: location
      map: @map
      icon: markerImage)

  defineUserLocation: () ->
    if navigator.geolocation
      navigator.geolocation.getCurrentPosition(
        (position) =>
          pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude)
          infowindow = new google.maps.InfoWindow(
            map: @map
            position: pos
            content: 'Если это не ваше местоположение - передвиньте маркер'
          )
          @map.setCenter(pos)
          @userLocationMarker = @placeMarker(pos, null)
      )
    alert @userLocationMarker.getPosition()

到底为什么我在这一点上有一个居中的map和maker,但是@userLocationMarker未定义,getPosition方法调用错误?

navigator.geolocation.getCurrentPosition
是一个异步函数。它的回调(在这里设置
@userLocationMarker
)在
警报
调用后运行。例如,您也可以将您的
警报
行放入回调中。

但是我在class@koshak1993:啊,那必须是
userLocationMarker:null
。我不明白。Why(position)函数可以看到变量映射,但看不到userLocationMarker当您编写
userLocationMarker=
时,您正在定义一个具有该名称的变量,该名称仅存在于由
class Window.MapHandler
创建的函数体中—一个私有变量。改为使用
使其成为类原型的属性。
@map
可见的原因是您在其他地方编写了
@map=…
@map
与您的
map=
声明无关。看到了吗?