javascript - 未捕获的 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,但如果有请在评论中告诉我。
解决方案
Javascript 是一种区分大小写的语言,您在使用时必须使用完全相同的语言关键字、函数、变量等名称。
所以你的函数getDomstrings
在调用时无效getDOMstrings
。
您必须调用函数 asgetDomstrings
或将函数名称更改为getDOMstrings
然后才能调用 as getDOMstrings
。
所以你的以下代码:
var DOM = UICtrl.getDOMstrings();
应该:
var DOM = UICtrl.getDomstrings();
推荐阅读
- c++ - 如何用P1787解释内联函数中的静态局部变量为什么引用同一个对象
- java - 使用 Admin Console API 在 keycloak 中导出具有凭据的用户
- exchangewebservices - 无法使用 exchagelib 代码删除房间日历项目
- c++ - 如何以特定顺序将数字存储在两个数组中?
- sql - SQL获取一个单词只出现一次的列中的字段
- dm-script - 4D 数据选择器工具 (2D x 2D)
- python - 具有特定条件的时间戳的 UniqueID
- vim - Vim 打开终端的配置文件与默认配置不同
- angular - Can we send value as Y if checkbox is checked and value as N if checkbox is unchecked in angular?
- python - heroku ps:scale web=1 缩放测功机...找不到该进程类型(网络)