我有以下内容:
$(document).ready ->
root = exports ? this
root.hello = -> 'hello world'
world = ->
root.hello
alert world
弹出警告信息:
function() {
root.hello }
我想让它弹出“你好世界”。如何从函数中返回coffeescript全局变量?
答案 0 :(得分:2)
你必须做的两件事:
首先,删除
中的->
root.hello = -> 'hello world'
// ^^ remove this
->
表示您已将功能分配给root.hello
。
然后,您需要在拨打电话后()
之后添加world
,这样您就可以调用 world
,而不只是引用它
alert world()
// ^^ Add these
所以:
$(document).ready ->
root = exports ? this
root.hello = 'hello world'
world = ->
root.hello
alert world()