3

引数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'

更新:これが私が現在持っているJavaScriptで、CoffeeScriptに変換しようとしています。

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');
     }
    }
  });
 }
4

1 に答える 1

19

ここにあるのは、という名前の関数ではありませんfreeSample。という単一の引数を持つ無名関数freeSampleです。CoffeeScript の関数の構文は次のようになります。

myFunctionName = (myArgument, myOtherArgument) ->

したがって、あなたの場合は次のようになります。

freeSample = (market) ->
  #Whatever

編集(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/

このツールによって生成された上記のスニペット。

于 2012-06-19T11:33:23.450 に答える