我有一个CoffeeScript类:
class window.MapHandler
map = null
makeMap: () ->
latlng = new google.maps.LatLng(54.711929,20.5089);
myOptions =
zoom: 12
center: latlng
mapTypeId: google.maps.MapTypeId.ROADMAP
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions)
@geocode("Калининград, Чернышевского 101")
placeMarker: (location) ->
marker = new google.maps.Marker(
position: location
map: @map)
geocode: (address) ->
geocoder = new google.maps.Geocoder
geocoder.geocode(
'address': address,
(results, status) ->
if status is google.maps.GeocoderStatus.OK
map.setCenter(results[0].geometry.location)
@placeMarker(results[0].geometry.location)
else alert("Geocode was not successful for the following reason: " + status);
)
当我从地理编码方法的匿名函数调用placeMarker方法时出现问题:visualizer.js:37Uncaught TypeError:Object [object DOMWindow]没有方法'placeMarker'
如何调用此方法?
答案 0 :(得分:4)
geocode: (address) ->
geocoder = new google.maps.Geocoder
geocoder.geocode(
'address': address,
(results, status) =>
if status is google.maps.GeocoderStatus.OK
map.setCenter(results[0].geometry.location)
@placeMarker(results[0].geometry.location)
else alert("Geocode was not successful for the following reason: " + status);
)
请注意第5行中的胖箭头 - 它会在闭包内保留this
和@
。