2012-04-25 129 views
1

我在轨道上制作了一个红宝石应用程序,这是我的一个咖啡脚本文件
我相信我的代码缩进了,但我仍然收到错误。
我标记了下面给出的注释错误。
请帮忙!Coffeescript不能正确转换

jQuery -> 


    today_date = new Date() 
    month = today_date.getMonth() 
    day = today_date.getDay() 


    pkpstyle= [ 
     featureType: "landscape.natural" 
     elementType: "geometry" 
     stylers: [ 
      lightness: -29 
     , 
      hue: "#ffee00" 
     , 
      saturation: 54 
     ] 
    , 
     featureType: "poi.park" 
     stylers: [ 
      lightness: -35 
     , 
      hue: "#005eff" 
     ] 
    , 
     featureType: "road.arterial" 
    , 
     featureType: "road.arterial" 
     stylers: [ lightness: 45 ] 
    ] 


    tempDay = 4 
    //I get an error here saying Uncaught TypeError: undefined is not a function 
    today_latlng = getLatlng(stops[tempDay]) 

    markericon = new google.maps.MarkerImage("/assets/cycling.png") 
    myOptions = 
     center: today_latlng 
     zoom: 12 
     minZoom: 4 
     styles: pkpstyle 
     mapTypeId: google.maps.MapTypeId.ROADMAP 

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions) 
    for i of stops 
     latlng = getLatlng(stops[i].latlng) 
     marker = new google.maps.Marker(
      map: map 
      icon: markericon 
      position: latlng 
     ) 

    getLatlng = (loc) -> 
     loc_split = loc.split(", ") 
     lat = loc_split[0] 
     lng = loc_split[1] 
     new google.maps.LatLng(lat, lng) 
+0

getLatLong在那里评估为undefined。有什么问题? (Coffeescript是否允许像这样的前向声明?JavaScript中的FunctionStatements *而不是*“var f = func”语句允许前向声明.JS最终是什么?我想象成一个'var' ...因此' undefined'。) – 2012-04-25 23:20:36

+0

您是否将此代码从Javascript转换为CoffeeScript? – Jonathan 2012-04-25 23:22:27

回答

2

这CoffeeScript的:

today_latlng = getLatlng(stops[tempDay]) 
getLatlng = (loc) -> 
    loc_split = loc.split(", ") 
    lat = loc_split[0] 
    lng = loc_split[1] 
    new google.maps.LatLng(lat, lng) 

是,或多或少,与此相同的JavaScript:

var today_latlng, getLatLng; 
today_latlng = getLatLng(stops[tempDay]) 
getLatLng = function(loc) { ... }; 

所以,你有一个getLatLng可变的,当你getLatLng(stops[tempDay])但你在之后,您不要为其分配值,直到后,您尝试将其作为函数调用。您需要定义getLatLng作为一个函数,你把它作为前一个:

getLatlng = (loc) -> 
    loc_split = loc.split(", ") 
    lat = loc_split[0] 
    lng = loc_split[1] 
    new google.maps.LatLng(lat, lng) 
#... 
today_latlng = getLatlng(stops[tempDay]) 

而且,如果stops是一个数组,那么你不应该使用of循环,你应该使用一个in loop

for p in stops 
    latlng = getLatlng(p.latlng) 
    #... 

of循环是相同的for ... in JavaScript的循环,并且可以做有趣的事情有一个数组,一个in循环最终成为在JavaScript中的for(;;)循环而这很乖机智h阵列。我不知道stops是怎么回事,所以这可能不适用,我只是猜测它是如何使用以及i指数变量。