javascript - Javascript Uncaught Typeerror - 不是构造函数
问题描述
我目前在 mvc 模式中编写一个项目。但我收到一条奇怪的错误消息,我无法解决。请注意,我的编辑器(Atom,即使添加了 Linter)不会抱怨我的代码。而且我现在在网上找不到类似的东西,因为大多数时候它只是简单的订单错误,但我不认为我在这里犯了这些错误。我使用 chrome 来测试我的代码。
/* eslint-env browser */
/* global request */
var Countdown = (function() {
"use strict";
var that = {},
countdownController,
countdownView;
function onStartGamePressed(){
countdownView.hideMenu();
}
function init() {
// Die Initalisierung der Anwendung beginnt hier
/* Die Anfrage an die Wiktionary-API können Sie nach diesem Muster gestalten
* Die Methode request findet sich im globalen Scope und wird durch Einbindung
* der Datei request.js bereitgestellt.
*/
initCountdownController();
initCountdownView();
request({
success: onWiktionaryResponseAvailable,
error: null,
url: "https://en.wiktionary.org/w/api.php?action=query&origin=*&format=json&titles=student",
});
}
function initCountdownController(){
countdownController = (new Countdown.CountdownController({
//startGame: document.querySelector(".button start-game")
})).init();
countdownController.setOnStartGamePressedListener(onStartGamePressed);
}
function initCountdownView(){
countdownView = (new Countdown.CountdownView({})).init();
}
function onWiktionaryResponseAvailable(result) {
var obj;
console.log(result);
obj = JSON.parse(result);
console.log(obj);
}
that.init = init;
return that;
}());
我得到了两个序列的错误(新倒计时....)提前谢谢!
/* global Countdown */
Countdown.CountdownView = function(){
"use strict";
var that = {};
function init(){
return that;
}
function hideMenu(){
document.getElementById("menu-screen").className = "hidden";
}
that.init = init;
that.hideMenu = hideMenu;
return that;
};
添加引用CountdownView.js的示例
解决方案
您没有为 Countdown.CountdownController 和 Countdown.CountdownView 定义任何构造函数,因此该行将new Countdown.CountdownView()
抛出Countdown.CountdownController()
您的错误。
编辑:
您需要在 CountdownView.js 和 CountdownController.js 中导出/导入您的定义。
此外,我会更改Countdown.CountdownView = ...
by的定义CountdownView = ...
并替换您从中实例化它的new Countdown.CountdownView(...)
行new CountdownView(...)
推荐阅读
- c# - 我正在尝试使用 c# 从 Twitter api 获取请求令牌,但收到 400 错误请求错误
- android - 套接字连接丢失,前台服务在一段时间后被操作系统杀死
- java - Java 多连接查询
- angular - 如何在角度中使用事件冒泡
- java - java.lang.IllegalStateException: android.media.MediaPlayer.stop
- javascript - 在VueJS 2中通过v-for中的道具将数据从父级传递给子级
- c# - 成功读取c#中的数据后,如何在csv文件中显示mysql数据
- sql - 如何在 SQL Sever 中拆分同一张表中的两列
- c# - 如何在 Windows 窗体中将数据库中的值显示到 ComboBox?
- xml - 视图负责 Odoo12 中的顶部导航栏