简体   繁体   English

无法从类外部访问我的coffeescript类中的方法

[英]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: 您缺少几件事:

  1. The Notification in your SharerController is not the same as the Notification you're defining in Notification.js.coffee . NotificationSharerController是不一样的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方法。
  2. There is no @dom in your Notification so your display call will fail if you ever manage to call it. 您的Notification没有@dom ,因此如果您设法进行呼叫,则display呼叫将失败。
  3. There is no 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM