有什么办法不使用CoffeeScript返回东西? [英] Is there any way to not return something using CoffeeScript?

查看:126
本文介绍了有什么办法不使用CoffeeScript返回东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎CoffeeScript自动返回范围中的最后一个项目。我可以避免这个功能吗?

It seems like CoffeeScript automatically returns the last item in a scope. Can I avoid this functionality?

推荐答案

你必须显式返回任何内容,或者留下一个表达式计算为undefined在底部您的函数:

You have to explicitly return nothing, or to leave an expression evaluating to undefined at the bottom of your function:

fun = ->
    doSomething()
    return

或:

fun = ->
    doSomething()
    undefined

comprehensions:

This is what the doc recommends, when using comprehensions:


请注意,在这些情况下,您不会不小心返回理解结果,添加有意义的返回




$ b
$ b

然而,你可以这样写一个包装:


You could, however, write a wrapper like this:

voidFun = (fun) ->
    ->
        fun(arguments...)
        return

href =http://jashkenas.github.com/coffee-script/#splats> splat operator ( ... ))

(Notice the splat operator here (...))

在定义函数时使用它:

fun = voidFun ->
    doSomething()
    doSomethingElse()

>

Or like this:

fun = voidFun(->
    doSomething()
    doSomethingElse()
)

这篇关于有什么办法不使用CoffeeScript返回东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆