如何在浏览器中使用 CoffeeScript 正确确定函数范围?

发布于 2024-12-20 03:52:17 字数 440 浏览 2 评论 0原文

(->
  jQuery ->
    globalThis = @
    $('#emailTypes').change ->
      globalThis.toggleEmailOptions()

  toggleEmailOptions = ->
    $('.emailTypeOptions').fadeOut 'fast', ->
      for emailType in $('#emailTypes').val()
        $("##{emailType}Options").fadeIn()


).call this

这是我的 .coffee 文件。但是,toggleEmailOptions 函数不在 jQuery ready 的范围内。我怎样才能使其在范围内?

(->
  jQuery ->
    globalThis = @
    $('#emailTypes').change ->
      globalThis.toggleEmailOptions()

  toggleEmailOptions = ->
    $('.emailTypeOptions').fadeOut 'fast', ->
      for emailType in $('#emailTypes').val()
        $("##{emailType}Options").fadeIn()


).call this

That's my .coffee file. However, the toggleEmailOptions function isn't in scope of the jQuery ready. How can I make it in scope?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一影成城 2024-12-27 03:52:17

来自 http://jashkenas.github.com/coffee-script/

有关 CoffeeScript 的常见警告适用 - 您的内联脚本将
在闭包包装器中运行,所以如果你想公开全局
变量或函数,将它们附加到窗口对象。

或者如果您使用的是 node.js ,则绑定到 exports

对于你的例子,它变成:

window.toggleEmailOptions = ->

From http://jashkenas.github.com/coffee-script/

The usual caveats about CoffeeScript apply — your inline scripts will
run within a closure wrapper, so if you want to expose global
variables or functions, attach them to the window object.

Or bind to exports if you're using node.js .

For your example, it becomes:

window.toggleEmailOptions = ->
夜雨飘雪 2024-12-27 03:52:17

我认为你不需要换行,因为咖啡脚本已经做到了,这是我重写你的代码的方式:

jQuery ->
  toggleEmailOptions = ->
    $('.emailTypeOptions').fadeOut 'fast', ->
      for emailType in $('#emailTypes').val()
        $("##{emailType}Options").fadeIn()

  $('#emailTypes').change ->
    toggleEmailOptions()

I think you don't need to wrap because coffeescript already do it, here's how I would rewrite your code:

jQuery ->
  toggleEmailOptions = ->
    $('.emailTypeOptions').fadeOut 'fast', ->
      for emailType in $('#emailTypes').val()
        $("##{emailType}Options").fadeIn()

  $('#emailTypes').change ->
    toggleEmailOptions()
等风也等你 2024-12-27 03:52:17

这里有几个问题。一是您似乎假设 @/this 是 jQuery 回调中的全局对象 (window)。但很容易看出事实并非如此:

jQuery -> console.log @ is window  # false

请记住,可以在库想要的任何上下文中调用回调。在本例中,@文档。并且 document.toggleEmailOptions 不存在。

此外,如果您希望 toggleEmailOptions 成为全局变量,则需要将其附加到 window (或 @/this,因为 @ 是文件最外层范围中的 window)。这是因为当您只编写 toggleEmailOptions = ... 时,CoffeeScript 使用 var 声明将其范围限定在文件内。通过查看编译后的 JS 很容易看出这一点:var 始终位于其作用域的顶部。幸运的是,文件范围无论如何都是您想要的。

所以基本上:不要想太多!你想要的是:

jQuery ->
  $('#emailTypes').change ->
    toggleEmailOptions()

其余的保持原样。

There are a couple of problems here. One is that you seems to be assuming that @/this is the global object (window) from the jQuery callback. But it's easy to see that it isn't:

jQuery -> console.log @ is window  # false

Remember that callbacks can be called in whatever context the library wants. In this case, @ is document. And document.toggleEmailOptions doesn't exist.

Further, if you want toggleEmailOptions to be a global, you need to attach it to window (or @/this, since @ is window in the outermost scope of your file). That's because when you just write toggleEmailOptions = ..., CoffeeScript scopes it within the file using a var declaration. It's easy to see this by looking at the compiled JS: vars are always at the top of their scope. Fortunately, file scope is what you wanted anyway.

So basically: Don't overthink it! What you want is:

jQuery ->
  $('#emailTypes').change ->
    toggleEmailOptions()

and leave the rest as it is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文