访问 CoffeeScript 默认包装器中的文档
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
包装器不会隐藏
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 towindow
. Unless you've declared a variable nameddocument
within your.coffee
file (by writingdocument = ...
),document
will be accessible fromsomeMethod
. Try addingconsole.log document
to the top ofsomeMethod
to check for yourself.So there must be something else going on. What exactly is the error message you get when
someMethod
is called?只需执行
window.publicObject.someMethod()
即可。这避免了整个变量this
范围问题。您可以在 CS 代码的顶级范围中互换使用this
和window
,但是一旦进入函数内部,您就需要使用window
代码>.我建议一直使用window
,因为它的A)更清晰,B)回避了整个这个
问题,这个问题导致了无数个小时的头痛。另外,这是我使用的命名空间模式。我创建了 1 个顶级全局对象,并将所有内容整齐地悬挂在该对象上。事情是这样开始的。
Just do
window.publicObject.someMethod()
. This avoids the whole variablethis
scope issue. You can usethis
andwindow
interchangeably in the top level scope of you CS code, but once you get inside functions, you'll need to usewindow
. I suggest usingwindow
all the time as its A) clearer and B) sidesteps the wholethis
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.