javascript CoffeeScript 函数参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11099526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:05:27  来源:igfitidea点击:

CoffeeScript function argument

javascriptcoffeescript

提问by pertrai1

I have a function that I want to pass an argument, market, to the function freeSample, but I can't seem to get it set as an argument. Please take a moment to look at my code and help me to understand how to get the market as an argument in the freeSample function.

我有一个函数,我想将参数市场传递给函数 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'

UPDATE: Here is the JavaScript I currently have that I am trying to convert to CoffeeScript.

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

回答by Alladinian

What you have here is not a function named freeSample. Is an anonymous function with a single argument called freeSample. The syntax for functions in CoffeeScript is like this:

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

myFunctionName = (myArgument, myOtherArgument) ->

So in your case it could be something like this:

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

freeSample = (market) ->
  #Whatever

EDIT(after OP updated the question): In your specific case you could do it like so:

编辑(在 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. There is an (awesome) online tool for converting between js/coffeescript and can be found here: http://js2coffee.org/

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

The above snippet generated by this tool.

此工具生成的上述代码段。