首页 > 解决方案 > 未捕获的 TypeError:UICtrl.getDOMstrings 不是函数

问题描述

var budgetController = (function() {


})();

//UI CONTROLLER
var UIController = (function () {

    var DOMstrings = {
        //so i won't have to change '.add__type' for example everywhere in the code if i decide to modify my html
        inputType: '.add__type',
        inputDescription: '.add__description',
        inputValue: '.add__value',
        inputBtn: '.add__btn'

    };

    return {
      getInput: function(){
          return {
              type: document.querySelector(DOMstrings.inputType).value,//will be wither inc or exp
              description: document.querySelector(DOMstrings.inputDescription).value,
              value: document.querySelector(DOMstrings.inputValue).value
          };

      },
        getDomstrings: function() {
            //exposing the domstring object to the public
            return DOMstrings;
        }
    };

})();


//GLOBAL APP CONTROLLER
var controller = (function(budgetCtrl,UICtrl) {

    var DOM = UICtrl.getDOMstrings();

    var ctrlAddItem = function () {

        //1. get the field input data
        var input = UICtrl.getInput();
        console.log(input);

        //2.Add the item to the budget controller

        //3.Add the item to the UI

        //4. Calculate the budget

        //5. Display the budget on the UI


    }

    document.querySelector(DOM.inputBtn).addEventListener('click',ctrlAddItem);

    document.addEventListener('keypress', function(event) {
        // enter has that key code(13)
        if (event.keyCode === 13 || event.which === 13) {
            ctrlAddItem();
        }

    });

})(budgetController,UIController);

我在控制台中遇到的确切错误是:

app.js:39 Uncaught TypeError: UICtrl.getDOMstrings is not a function
    at app.js:39
    at app.js:68
(anonymous) @ app.js:39
(anonymous) @ app.js:68

积极学习 javascript 并陷入了一个没有意义的错误,因为.getDomstrings()上面定义的函数和 public 所以应该可以访问它。并且可能不需要添加 html,但如果有请在评论中告诉我。

标签: javascriptdomclosures

解决方案


Javascript 是一种区分大小写的语言,您在使用时必须使用完全相同的语言关键字、函数、变量等名称。

所以你的函数getDomstrings在调用时无效getDOMstrings

您必须调用函数 asgetDomstrings或将函数名称更改为getDOMstrings然后才能调用 as getDOMstrings

所以你的以下代码:

var DOM = UICtrl.getDOMstrings();

应该:

var DOM = UICtrl.getDomstrings();


推荐阅读