CoffeeScript函数参数

3
我有一个函数,想把参数market传递给freeSample函数,但我似乎无法将其设置为参数。请花点时间查看我的代码,并帮助我了解如何在freeSample函数中将market作为参数传递进去。
(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代码吗? - Subodh
1个回答

19

你这里没有一个名为freeSample的函数。而是一个带有单个参数freeSample的匿名函数。在CoffeeScript中,函数的语法如下:

myFunctionName = (myArgument, myOtherArgument) ->

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

freeSample = (market) ->
  #Whatever

编辑(在问题更新后):

在你的特定情况下,你可以这样做:

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/

以上代码段是由该工具生成的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接