【发布时间】:2014-08-17 23:16:49
【问题描述】:
在定义模块模式时,我对一个问题感到很困惑 (http://robots.thoughtbot.com/module-pattern-in-javascript-and-coffeescript):
以下代码可以作为 exepted (CoffeeScript) 工作:
window.Map = {}
Map.handle = ( ->
handle = 'some text'
print: () ->
console.log handle
)()
现在,如果我用全局范围内可用库中的方法替换 'some text'(即 gmaps4rails:https://github.com/apneadiving/Google-Maps-for-Rails):
window.Map = {}
Map.handle = ( ->
handle = Gmaps.build('Google')
print: () ->
console.log handle
)()
代码不起作用并抛出 Map.handle 未定义。所以我认为这是范围的问题,所以我尝试将Gmaps.build('Google') 作为参数传递给匿名函数,但失败了。
Gmaps.build 自做以来就可以正常工作:
window.Map = {}
Map.handle = ( ->
mapBuildFx = () ->
handle = Gmaps.build('Google')
)()
按预期工作。
我到底错过了什么?
【问题讨论】:
-
AFAIK,没有理由从给定的代码中抛出
Map.handle is undefined。你如何使用你的“单例”对象?
标签: javascript ruby-on-rails coffeescript gmaps4rails