首页 > 解决方案 > 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的示例

标签: javascriptconstructortypeerror

解决方案


您没有为 Countdown.CountdownController 和 Countdown.CountdownView 定义任何构造函数,因此该行将new Countdown.CountdownView()抛出Countdown.CountdownController()您的错误。

编辑:

您需要在 CountdownView.js 和 CountdownController.js 中导出/导入您的定义。

此外,我会更改Countdown.CountdownView = ...by的定义CountdownView = ...并替换您从中实例化它的new Countdown.CountdownView(...)new CountdownView(...)


推荐阅读