【发布时间】:2011-12-08 02:23:46
【问题描述】:
(->
jQuery ->
globalThis = @
$('#emailTypes').change ->
globalThis.toggleEmailOptions()
toggleEmailOptions = ->
$('.emailTypeOptions').fadeOut 'fast', ->
for emailType in $('#emailTypes').val()
$("##{emailType}Options").fadeIn()
).call this
这是我的.coffee 文件。但是,toggleEmailOptions 函数不在 jQuery ready 的范围内。我怎样才能使它在范围内?
【问题讨论】:
-
因为 toggleEmailOptions 无论如何都是一个局部变量,你可以将它移动到 jQuery.Ready 回调中。如果你想让 toggleEmailOptions 在全局范围内可用,你可以使用 => 代替 -> 来保留 this 内部函数并声明 @toggleEmailOptions 代替 toggleEmailOptions
标签: javascript coffeescript scope