[英]unable to access method in my coffeescript class from outside of class
I have this class written in CoffeeScript: 我有用CoffeeScript编写的此类:
Notification.js.coffee Notification.js.coffee
class Notification
display: ->
@dom.show()
constructor: (header, messages) ->
@render(header, messages)
Basically, the logic for render()
function code is to inject HTML into the DOM (but hidden) and display()
method simply shows
the DOM element. 基本上, render()
函数代码的逻辑是将HTML注入DOM(但被隐藏),而display()
方法仅shows
DOM元素。 Now, I have some other class separate to this one where I'm trying to make use of this above class. 现在,我有另外一个与该类分开的其他类,在这里我试图利用上述类。
SharerController.js.coffee SharerController.js.coffee
class SharerController
post_story: ->
# some user action posting something in the app
notification = new Notification('Header', ['This story has been posted.', 'You can post more. Would you like to?'])
notification.display()
Unfortunately, for some reason - I get 不幸的是,由于某种原因-我得到了
TypeError: 'undefined' is not a function (evaluating 'notification.display()')
on the line above where I do notification.display()
. 在我做notification.display()
的那一行上。 The same code works absolutely as expected If I write it within the Notification class (where everything gets wrapped into an IIFE). 如果我在Notification类(所有内容都包装到IIFE中)中编写,则相同的代码将完全按预期工作。 The load order for above files is: Notification.js and then SharerController.js 上述文件的加载顺序为:Notification.js,然后是SharerController.js
What exactly am I missing here? 我在这里到底想念什么?
You're missing several things: 您缺少几件事:
Notification
in your SharerController
is not the same as the Notification
you're defining in Notification.js.coffee
. 该Notification
在SharerController
是不一样的Notification
你定义Notification.js.coffee
。 I think you're picking up Chrome's native Notification
and that doesn't have a display
method. 我认为您正在使用Chrome的本机Notification
,但没有display
方法。 @dom
in your Notification
so your display
call will fail if you ever manage to call it. 您的Notification
没有@dom
,因此如果您设法进行呼叫,则display
呼叫将失败。 render
method in your Notification
so your Notification
constructor will fail due to the @render
call. Notification
没有render
方法,因此您的Notification
构造函数将由于@render
调用而失败。 If you're only including a sample of your Notification
code then (2) and (3) aren't really problems. 如果仅包含Notification
代码的示例,则(2)和(3)并不是真正的问题。
CoffeeScript wraps the generated JavaScript in a self-executing function something like this: CoffeeScript将生成的JavaScript包装在一个自执行函数中,如下所示:
(function() {
// JavaScript goes here...
}).call(this);
so your Notification
isn't visible outside your Notification.js.coffee
file. 所以您的Notification
是不是你外面看到Notification.js.coffee
文件。 You can make it globally visible by saying: 您可以通过以下方式使其在全球范围内可见:
class window.Notification
#...
or 要么
class @Notification
#...
or you could use your own namespace as in this other question . 或者您可以像其他问题一样使用自己的名称空间。
Once you've made your Notification
available to the rest of your application you can instantiate your own Notification
, solve your other problems, and finally you'll be calling display
on something that has a display
method. 在将Notification
用于其他应用程序后,您可以实例化自己的Notification
,解决其他问题,最后,您将在具有display
方法的对象上调用display
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.