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=
声明无关。看到了吗?