Coffeescript函数语法

时间:2012-05-21 19:46:26

标签: function coffeescript

我是Coffeescript的新手并且在语法方面苦苦挣扎。任何人都可以帮我解决如何在CS中编写以下内容吗?

$("#getLocation").click(function() {
  $('#location-loading').show();
  navigator.geolocation.getCurrentPosition(applyLocation);
  return false;
});

function applyLocation(location) {
  $('#LogLongitude').val(location.coords.longitude);
  $('#LogLatitude').val(location.coords.latitude);
  alert('Latitude:' + location.coords.latitude + ', Longitude: ' + location.coords.longitude + ', Accuracy: ' + location.coords.accuracy);
  $('#location-loading').hide();
}

我认为以下内容可行,但我在调用函数时返回错误(因此我没有按照链接)。

$('#getLocation').click ->
  $('#location-loading').show()
  navigator.geolocation.getCurrentPosition(applyLocation)
  false

applyLocation = (location) ->
  $('#LogLongitude').val(location.coords.longitude)
  $('#LogLatitude').val(location.coords.latitude)
  alert('Latitude:' + location.coords.latitude + ', Longitude: ' + location.coords.longitude + ', Accuracy: ' + location.coords.accuracy)
  $('#location-loading').hide()

1 个答案:

答案 0 :(得分:1)

你可以省略简单函数调用的() parethesis(不链接) 并将下部的字符串放入双配额中,以便能够使用#{}语法, 但除了你的代码确实看起来很咖啡;)

$('#getLocation').click ->
  $('#location-loading').show()
  navigator.geolocation.getCurrentPosition applyLocation
  false

applyLocation = (location) ->
  coords = location.coords
  $('#LogLongitude').val coords.longitude
  $('#LogLatitude').val coords.latitude
  alert "Latitude: #{coords.latitude}, 
         Longitude: #{coords.longitude}, 
         Accuracy: #{coords.accuracy}"
  $('#location-loading').hide()