我創建了裏面的一些異步JS代碼的對象創建的對象Execute方法。當創建對象時,我想調用一個方法。我有點難倒我如何可以引用新對象來調用它的方法。的Javascript:由回調
這裏是我的CoffeeScript。該代碼將標記添加到Google地圖中:
addLocation: (name, id, lat, lng, options, callback) ->
# Add a new location to the map
#
# @param string name - name to give the location
# @param int id - ID of the location if stored in the database already
# @param double lat - latitude
# @param double lng - longitude
# @param json options - options to use while adding the location
console.log("Adding new location '#{name}' (#{lat}, #{lng}) to map") if debug
@__setVisible true, =>
location = new Location(this, id, name)
location.setLatLng(lat, lng, options)
@locations.add(location)
callback() if callback
return location
而這裏是我調用此方法的地方。我想在返回的'location'對象上調用一個方法,但是如何在回調中綁定一個尚未實例化的對象呢?
__addLocation: (resultLocation) ->
# Add a new location to the map and centre the view in on it
name = $(@nameElement).val()
lat = resultLocation.geometry.location.lat()
lng = resultLocation.geometry.location.lng()
@map.addLocation name, null, lat, lng, { draggable: true }, ->
# location doesn't exist at this point so the following line fails
location.setShowInfoWindowOnClick(true)
@map.centerMap(location)
我該如何執行location.setShowInfoWindowOnClick(true)?
這是最奇怪的Javascript我都看到了。 –
哦 - 我看到,coffescript –