2012-06-19 75 views
3

我有一個函數需要將參數市場傳遞給函數freeSample,但我似乎無法將其設置爲參數。請花一點時間查看我的代碼,並幫助我瞭解如何在freeSample函數中將市場作爲參數。CoffeeScript函數參數

(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'); 
    } 
    } 
    }); 
} 
+0

你能否也提供你的JS代碼? – Subodh

回答

17

這裏有什麼不是一個名爲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/

以上代碼片段由此工具生成。

+0

是的,我補充說,我的測試,它的工作。謝謝。 – pertrai1

相關問題