CoffeeScript函数参数

per*_*ai1 4 javascript coffeescript

我有一个函数,我想将一个参数,市场,传递给函数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'
Run Code Online (Sandbox Code Playgroud)

更新:这是我目前正在尝试转换为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');
     }
    }
  });
 }
Run Code Online (Sandbox Code Playgroud)

All*_*ian 19

你在这里拥有的不是一个名为的函数freeSample.是一个带有单个参数的匿名函数freeSample.CoffeeScript中函数的语法如下:

myFunctionName = (myArgument, myOtherArgument) ->
Run Code Online (Sandbox Code Playgroud)

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

freeSample = (market) ->
  #Whatever
Run Code Online (Sandbox Code Playgroud)

编辑(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"
Run Code Online (Sandbox Code Playgroud)

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

以上代码段由此工具生成.