访问 CoffeeScript 默认包装器中的文档

发布于 2024-11-27 22:27:24 字数 938 浏览 1 评论 0原文

Coffeescript 将您的代码包装到像这里这样的包装器中

(function() { 
/* your code */
}).call(this);

this 表示 window。因此,要创建一个公共接口,我

this.publicObject =
  someMethod: ->
    document.getElementById("button1").innerHTML = "Changed!"

会执行类似的操作,然后在 HTML 文档中注册一个回调,使用 Click

但是,如果我想从 .coffee 文件调用 someMethod (在文档准备好时调用,我认为 编辑:请参阅下面接受的答案+评论)怎么办?如果我只是按照上面的代码进行操作,

publicObject.someMethod()

由于上下文问题,似乎在 someMethod 中无法访问文档对象。 如何从我的 .coffee 文件中调用 publicObject.someMethod() 并让它识别 document

注意:apply()< /code> 和 call() 欺骗是可以的,但如果可能的话,我不想摆脱包装器。如果您关心,我使用以下内容来编译我的脚本:

coffee -j -p -c coffee/*.coffee > www/app.js

Coffeescript wraps your code into a wrapper like

(function() { 
/* your code */
}).call(this);

Here, this means window. So, to create a public interface I do something like

this.publicObject =
  someMethod: ->
    document.getElementById("button1").innerHTML = "Changed!"

I can then register a callback in the HTML document invoking my .js file with something like <span onclick="publicObject.someMethod();">Click</span>.

However, what if I wanted to call someMethod from the .coffee file (to be called on document ready, I think EDIT: See accepted answer + comments below)? If I just follow the above code up with

publicObject.someMethod()

it seems like the document object is not accessible within someMethod due to context issues. How can I call publicObject.someMethod() from my .coffee file and have it recognize document?

Note: apply() and call() trickery is OK, but I don't want to get rid of the wrapper, if possible. If you care, I use the following to compile my script:

coffee -j -p -c coffee/*.coffee > www/app.js

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

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

发布评论

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

评论(2

看春风乍起 2024-12-04 22:27:24

包装器不会隐藏 document,它是全局的,因为它附加到 window。除非您在 .coffee 文件中声明了名为 document 的变量(通过编写 document = ...),document< /code> 可以从 someMethod 访问。尝试将 console.log 文档 添加到 someMethod 的顶部来自行检查。

所以一定还有其他事情发生。调用 someMethod 时收到的错误消息到底是什么?

The wrapper doesn't hide document, which is a global because it's attached to window. Unless you've declared a variable named document within your .coffee file (by writing document = ...), document will be accessible from someMethod. Try adding console.log document to the top of someMethod to check for yourself.

So there must be something else going on. What exactly is the error message you get when someMethod is called?

筱果果 2024-12-04 22:27:24

只需执行window.publicObject.someMethod()即可。这避免了整个变量 this 范围问题。您可以在 CS 代码的顶级范围中互换使用 thiswindow,但是一旦进入函数内部,您就需要使用 window代码>.我建议一直使用window,因为它的A)更清晰,B)回避了整个这个问题,这个问题导致了无数个小时的头痛。

另外,这是我使用的命名空间模式。我创建了 1 个顶级全局对象,并将所有内容整齐地悬挂在该对象上。事情是这样开始的。

OT = window.OT = {} #root of the public API namespace
OT.someNestedPublicObj = {}

Just do window.publicObject.someMethod(). This avoids the whole variable this scope issue. You can use this and window interchangeably in the top level scope of you CS code, but once you get inside functions, you'll need to use window. I suggest using window all the time as its A) clearer and B) sidesteps the whole this issue which has caused countless hours of head scratching.

Also, this is the namespace pattern I use. I create 1 top-level global object and hang everything off of that neatly. It starts like this.

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