我有一个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‘
如何调用此方法?
发布于 2011-11-07 04:53:30
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和@。
https://stackoverflow.com/questions/8030277
复制相似问题