首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从闭包函数调用方法

从闭包函数调用方法
EN

Stack Overflow用户
提问于 2011-11-07 04:48:59
回答 1查看 229关注 0票数 1

我有一个CoffeeScript类:

代码语言:javascript
复制
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‘

如何调用此方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-07 04:53:30

代码语言:javascript
复制
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@

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8030277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档