【问题标题】:TypeError: EventList is not a constructor in one place, in another code worksTypeError:EventList 在一个地方不是构造函数,在另一个代码中有效
【发布时间】:2016-12-08 09:52:55
【问题描述】:

奇怪的情况,我有 django (1.8) 应用程序在 js 中具有功能。在一个地方一切正常,但在另一个地方我得到一个错误:

Uncaught TypeError: EventList is not a constructor

js文件中的代码:

var EventList=function(){};
EventList.prototype.init=function(){this.$eventslist=$(".event-list")
...

当我尝试使用 eventList = EventList(); 通话时,我收到消息:Uncaught TypeError: EventList is not a function

HTML 文件中的代码,在 js 代码块中:

<script src="{% static 'js/event_list.min.js' %}" type="text/javascript"></script>
<script type="text/javascript">
    eventList = new EventList();
    eventList.init();
</script>

【问题讨论】:

  • EventList 函数上是否设置了构造函数?我不知道你到底从那个函数返回了什么,但你可以直接将它作为一个函数调用而不是实例化它,因为它似乎没有原型:eventList = EventList();
  • @aks 请检查我的编辑
  • 你从哪里知道它不是构造函数?你能把代码也加进去吗?
  • 在这个地方我得到信息,这不是构造函数 eventList = new EventList(); 删除后 new 我得到的信息 EventList 不是一个函数。

标签: javascript django frontend


【解决方案1】:

你试过了吗

<script type="text/javascript">
     document.addEventListener('DOMContentLoaded', function() {   var
     var eventList = new EventList();
     eventList.init(); 
    });
</script>

也许你需要等待文档准备好或者你需要“var”你的事件列表

【讨论】:

    猜你喜欢
    • 2019-09-27
    • 2016-07-03
    • 2013-11-29
    • 2014-08-01
    • 2010-09-22
    • 2019-11-05
    • 1970-01-01
    相关资源
    最近更新 更多