【问题标题】:Convert javascript code into coffescript将javascript代码转换为coffeescript
【发布时间】:2014-12-13 21:48:57
【问题描述】:

我目前正在使用 Dashing,我需要将 javascript 代码转换为 coffescript 才能使用人力车图形库。 (根据本站源代码http://code.shutterstock.com/rickshaw/examples/x_axis.html

我正在尝试在图表上自定义 x 轴。

默认咖啡脚本代码=x_axis = new Rickshaw.Graph.Axis.X(graph: @graph)

要转换的JS代码

var format = function(n) {

    var map = {
        0: 'zero',
        1: 'first',
        2: 'second',
        3: 'third',
        4: 'fourth'
    };

    return map[n];
}

var x_ticks = new Rickshaw.Graph.Axis.X( {
    graph: graph,
    tickFormat: format
} );

进入咖啡脚本

    var format = function(n) {

    var map = {
        0: 'zero',
        1: 'first',
        2: 'second',
        3: 'third',
        4: 'fourth'
    };

    return map[n];
}


    x_axis = new Rickshaw.Graph.Axis.X(graph: @graph,tickFormat: format)//Make use of format

【问题讨论】:

  • 您究竟需要什么帮助?更重要的是,你想要什么?
  • 我希望能够使用 x_axis = new Rickshaw.Graph.Axis.X(graph: @graph,tickFormat: format) ,那么 format 函数应该是什么样子才能看起来像 coffescript?
  • 那没有帮助..有什么问题?为什么不能用人力车?
  • 我希望能够使用自定义的x轴
  • 我在 Coffescript 中使用人力车。所以我需要知道如何定义一个函数的语法,该函数完全符合 coffescript 语法中的格式

标签: javascript coffeescript rickshaw


【解决方案1】:
format = (n)->

  map =
    0: 'zero',
    1: 'first',
    2: 'second',
    3: 'third',
    4: 'fourth'
  map[n]

【讨论】:

  • 只要去掉javascript中所有多余的语法,你就有了coffeescript。
猜你喜欢
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-26
  • 2020-07-07
  • 1970-01-01
  • 2012-03-20
相关资源
最近更新 更多