在 document.ready 上运行 CoffeeScript 类

发布于 2024-10-11 09:20:28 字数 385 浏览 0 评论 0原文

我有

class Main
   test:->
      alert "yay!"

coffeescript,我想在我的index.html 中运行它,

<script>
    $(function(){
        //and obv Main.test(); doesn't work
    });
</script>

网站上有一个注释,它说它不起作用。但我找不到如何让它发挥作用。有什么想法吗?我需要找出 Coffeescript 闭包包装器是什么。

或者 Coffeescript 是否在 document.ready 之后执行?

谢谢!

i have

class Main
   test:->
      alert "yay!"

in coffeescript, and i want to run that inside my index.html

<script>
    $(function(){
        //and obv Main.test(); doesn't work
    });
</script>

there is a note of this on the website, it says it wouldn't work. But I couldn't find how to make it work. any ideas? i need to find out what coffeescript closure wrapper is.

or does coffeescript execute after document.ready anyway?

thx!

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

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

发布评论

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

评论(4

夏了南城 2024-10-18 09:20:28

要在 document.ready 之后执行 Coffeescript,您可以像这样使用 jQuery:

$ ->
  # Put your function code here
  init()

所做的就是运行 jQuery(function () {callback... }),就像此链接的第三部分一样:
http://api.jquery.com/jQuery/

基本上是这样说的:

jQuery(callback) 返回:jQuery
描述:绑定一个在 DOM 加载完成时执行的函数。

我在文档之外声明所有类等,然后调用 init 函数以使其在适当的时间运行。

我希望这有帮助!

To execute Coffeescript after document.ready you can use jQuery like this:

$ ->
  # Put your function code here
  init()

What that is doing is running jQuery(function () { callback... }) like the 3rd section at this link:
http://api.jquery.com/jQuery/

Which basically says this:

jQuery( callback ) Returns: jQuery
Description: Binds a function to be executed when the DOM has finished loading.

I declare all my classes, etc outside of document ready and then call an init function to get it running at the appropriate time.

I hope that helps!

断舍离 2024-10-18 09:20:28

主类

尝试使用 class @Main 代替。

obv Main.test();不起作用

对吧。应为 new Main().test()Main::test()

coffeescript 是否在 document.ready 之后执行?

假设您通过 extras/coffee-script.js 执行它 并使用 jQuery,是的。

class Main

Try class @Main instead.

obv Main.test(); doesn't work

Right. Should be new Main().test() or Main::test().

does coffeescript execute after document.ready anyway?

Assuming that you're executing it via extras/coffee-script.js and using jQuery, yes.

掀纱窥君容 2024-10-18 09:20:28

Coffeescript 将您的代码包装在函数调用中,这样您就不会意外覆盖全局变量。

如果您希望任何变量、函数或类成为全局变量(以便其他文件可以访问它们),则需要通过将它们附加到 thiswindow

# Stays within the function scope, so you can't access it outside the file
myNotGlobalFunction -> return

# Attaches it to `this` aka `window`, so can be accessed globally
this.myGlobalFunction -> return

# A shortcut using @ which is an alias to `this.`
@myOtherGlobalFunction -> return

这编译为:

(function() {
  myNotGlobalFunction(function() {
    return;
  });
  this.myGlobalFunction(function() {
    return;
  });
  this.myOtherGlobalFunction(function() {
    return;
  });
}).call(this);

Coffeescript wraps your code in a function call so you can't accidentally overwrite global variables.

If you want any variables, functions or classes to be global (so they can be accessed by other files), you need to explicitly make them global by attaching them to this or window.

# Stays within the function scope, so you can't access it outside the file
myNotGlobalFunction -> return

# Attaches it to `this` aka `window`, so can be accessed globally
this.myGlobalFunction -> return

# A shortcut using @ which is an alias to `this.`
@myOtherGlobalFunction -> return

This compiles to:

(function() {
  myNotGlobalFunction(function() {
    return;
  });
  this.myGlobalFunction(function() {
    return;
  });
  this.myOtherGlobalFunction(function() {
    return;
  });
}).call(this);
月朦胧 2024-10-18 09:20:28

我以前也遇到过这个问题。首先,由于您正在定义一个类,因此您需要实例化它。然后您可以在实例上调用 test 函数:

<script>
    $(function(){
        var an_instance_of_main = new Main();
        an_instance_of_main.test();
    });
</script>

但是,您可能已经注意到浏览器找不到您的 Main 类。这是因为,当编译 CoffeeScript 时,它会在类定义周围包装一个自执行函数,以防止 Main 被全局访问。如果你想让它全局可访问,你可以在它前面加上 window: 前缀

class window.Main
   test:->
      alert "yay!"

,或者在定义它之后赋值:

class Main
   test:->
      alert "yay!"

window.Main = Main

I've run into this issue before as well. First of all, since you are defining a class, you need to instantiate it. Then you can call you test function on the instance:

<script>
    $(function(){
        var an_instance_of_main = new Main();
        an_instance_of_main.test();
    });
</script>

However, you might have noticed that the browser can't find your Main class. This is because when the CoffeeScript is compiled, it wraps a self-executing function around your class definition in order to prevent Main from being globally accessible. If you want to make it globally accessible, you can prefix it with window:

class window.Main
   test:->
      alert "yay!"

or assign it after defining it:

class Main
   test:->
      alert "yay!"

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