Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在coffeescript中编写回调函数_Javascript_Jquery_Function_Callback_Coffeescript - Fatal编程技术网

Javascript 在coffeescript中编写回调函数

Javascript 在coffeescript中编写回调函数,javascript,jquery,function,callback,coffeescript,Javascript,Jquery,Function,Callback,Coffeescript,我在理解如何在coffeescript函数中编写回调函数时遇到了一些困难。我已经创建了这个脚本和几个函数 $('form').submit (e) -> console.log $(this) e.preventDefault() if $(this).hasClass 'volunteer-check-out-find-me-form' showFormLoader $(this), $(this).parent('aside.form').sib

我在理解如何在coffeescript函数中编写回调函数时遇到了一些困难。我已经创建了这个脚本和几个函数

$('form').submit (e) ->
    console.log $(this)
    e.preventDefault()
    if $(this).hasClass 'volunteer-check-out-find-me-form'
        showFormLoader $(this), $(this).parent('aside.form').sibling('.form-loader'), ->
            console.log 'finished'

showFormLoader = (form, formLoader, callback) ->
    'showing loader and hiding form'
    form.fadeOut ->
        formLoader.addClass('show').one 'webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', ->
            console.log 'calling back'
            callback()
但是当我提交表单时,控制台中出现了这个错误

未捕获类型错误:未定义不是函数


在函数调用打开的线路上,我是否做错了什么?有更好的方法吗?

Coffee脚本不允许像Javascript那样对函数进行正向引用,因此您需要将
showFormLoader
移动到
submit
处理程序上方。这样,声明在引用之前。顺便说一句,我认为这是Coffeescript的痛点

所以这是失败的:

baz(3)

baz = (n) ->
  console.log n
错误如下:

TypeError: undefined is not a function
  at Object.<anonymous> (untitled:51:3)
  at Object.<anonymous> (untitled:57:4)
  at Module._compile (module.js:456:26)

我希望我正确理解了你的问题。

你在哪一行得到错误?代码看起来很好。函数调用是在线的,我在这里传递参数和匿名函数,所以说
showFormLoader
不是函数?但是哪个函数调用?我在你的代码片段中计算了18次调用!调用代码段的showFormLoader第5行
baz = (n) ->
  console.log n

baz(3)