【问题标题】:CoffeeScript function argumentCoffeeScript 函数参数
【发布时间】:2012-06-19 10:56:48
【问题描述】:

我有一个函数,我想将参数 market 传递给函数 freeSample,但我似乎无法将它设置为参数。请花点时间查看我的代码并帮助我了解如何在 freeSample 函数中获取市场作为参数。

(freeSample) ->  
 market = $('#market')
  jQuery('#dialog-add').dialog =
   resizable: false
   height: 175
   modal: true
   buttons: ->
    'This is Correct': ->
      jQuery(@).dialog 'close'
    'Wrong Market': ->
      market.focus()
      market.addClass 'color'
      jQuery(@).dialog 'close'

更新:这是我目前正在尝试转换为 CoffeeScript 的 JavaScript。

function freeSample(market) 
 {
   var market = $('#market');
   jQuery("#dialog-add").dialog({
    resizable: false,
    height:175,
    modal: true,
     buttons: {
      'This is Correct': function() {
         jQuery(this).dialog('close');
     },
      'Wrong Market': function() {
        market.focus();
        market.addClass('color');
        jQuery(this).dialog('close');
     }
    }
  });
 }

【问题讨论】:

  • 能否也提供你的JS代码?

标签: javascript coffeescript


【解决方案1】:

您在这里拥有的不是名为freeSample 的函数。是一个匿名函数,带有一个名为 freeSample 的参数。 CoffeeScript 中函数的语法是这样的:

myFunctionName = (myArgument, myOtherArgument) ->

所以在你的情况下,它可能是这样的:

freeSample = (market) ->
  #Whatever

EDIT(在 OP 更新问题后): 在您的具体情况下,您可以这样做:

freeSample = (market) ->
  market = $("#market")
  jQuery("#dialog-add").dialog
    resizable: false
    height: 175
    modal: true
    buttons:
      "This is Correct": ->
        jQuery(this).dialog "close"

      "Wrong Market": ->
        market.focus()
        market.addClass "color"
        jQuery(this).dialog "close"

PS。有一个(很棒的)在线工具可以在 js/coffeescript 之间进行转换,可以在这里找到:http://js2coffee.org/

上面这个工具生成的sn-p。

【讨论】:

    猜你喜欢
    • 2013-06-10
    • 1970-01-01
    • 2013-12-25
    • 2012-02-17
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多