Javascript 未捕获类型错误:UICtrl.addListItem不是函数
我已经在这里坐了整整一个小时了,我不知道怎么了 我是新手,所以请不要评判Javascript 未捕获类型错误:UICtrl.addListItem不是函数,javascript,dom,error-handling,Javascript,Dom,Error Handling,我已经在这里坐了整整一个小时了,我不知道怎么了 我是新手,所以请不要评判 var budgetController = (function() { var Expense = function(id, description, value) { this.id = id; this.description = description; this.value = value; }; var Income = function
var budgetController = (function() {
var Expense = function(id, description, value) {
this.id = id;
this.description = description;
this.value = value;
};
var Income = function(id, description, value) {
this.id = id;
this.description = description;
this.value = value;
};
var data = {
allItems: {
exp: [],
inc: []
},
totals: {
exp: 0,
inc: 0
}
};
return {
addItem: function(type, des, val) {
var newItem, ID;
if (data.allItems[type].length > 0) {
ID = data.allItems[type][data.allItems[type].length - 1].id + 1;
} else {
ID = 0;
}
if (type === 'exp') {
newItem = new Expense(ID, des, val);
} else if (type === 'inc') {
newItem = new Income(ID, des, val);
}
data.allItems[type].push(newItem);
return newItem;
},
addListItem: function(obj, type) {
var html, newHTML, element;
if (type === 'inc') {
element = DOMstrings.incomesContainer;
html = '<div class="item clearfix" id="income-%id%"><div class="item__description">%description%</div><div class="right clearfix"><div class="item__value">%value%</div><div class="item__delete"><button class="item__delete--btn"><i class="ion-ios-close-outline"></i></button></div></div></div>';
} else if (type === 'exp') {
element = DOMstrings.expensesContainer;
html = '<div class="item clearfix" id="expense-%id%"><div class="item__description">%description%</div><div class="right clearfix"><div class="item__value">%value%</div><div class="item__percentage">21%</div><div class="item__delete"><button class="item__delete--btn"><i class="ion-ios-close-outline"></i></button></div></div></div>';
}
newHTML = html.replace('%id%', obj.id);
newHTML = html.replace('%description%', obj.description);
newHTML = html.replace('%value%', obj.value);
document.querySelector(element).insertAdjacentHTML('beforeend', newHTML);
}
};
})();
var UIController = (function() {
var DOMstrings = {
inputType: '.add__type',
inputDescription: '.add__description',
inputValue: '.add__value',
inputBtn: '.add__btn',
incomesContainer: '.income__list',
expensesContainer: '.expenses__list'
};
return {
getInput: function() {
return {
type: document.querySelector(DOMstrings.inputType).value,
description: document.querySelector(DOMstrings.inputDescription).value,
value: document.querySelector(DOMstrings.inputValue).value
};
},
getDOMstrings: function() {
return DOMstrings;
}
};
})();
var controller = (function(budgetCtrl, UICtrl) {
var setupEventListeners = function() {
var DOM = UICtrl.getDOMstrings();
document.querySelector(DOM.inputBtn).addEventListener('click', ctrlAddItem);
document.addEventListener('keypress', function(event) {
if (event.keyCode === 13 || event.which === 13) {
ctrlAddItem();
}
});
};
var ctrlAddItem = function() {
var input, newItem;
input = UICtrl.getInput();
newItem = budgetCtrl.addItem(input.type, input.description, input.value);
UICtrl.addListItem(newItem, input.type);
};
return {
init: function() {
setupEventListeners();
}
};
})(budgetController, UIController);
controller.init();
var预算控制器=(函数(){
var费用=功能(id、说明、值){
this.id=id;
this.description=描述;
这个值=值;
};
var收入=功能(id、说明、值){
this.id=id;
this.description=描述;
这个值=值;
};
风险值数据={
allItems:{
exp:[],
公司:[]
},
总数:{
实验:0,
公司:0
}
};
返回{
附加项:函数(类型、des、val){
var newItem,ID;
if(data.allItems[type].length>0){
ID=data.allItems[type][data.allItems[type].length-1].ID+1;
}否则{
ID=0;
}
如果(类型=='exp'){
新项目=新费用(ID、des、val);
}else if(类型==='inc'){
新项目=新收入(ID、des、val);
}
data.allItems[type].push(newItem);
返回新项目;
},
addListItem:函数(对象,类型){
var-html,newHTML,元素;
如果(类型=='inc'){
元素=DOMstrings.incomesContainer;
html=“%description%%value%”;
}else if(type==='exp'){
元素=DOMstrings.expensesContainer;
html=“%description%%值%21%”;
}
newHTML=html.replace(“%id%”,obj.id);
newHTML=html.replace(“%description%”,obj.description);
newHTML=html.replace(“%value%”,obj.value);
document.querySelector(element.insertAdjacentHTML('beforeend',newHTML);
}
};
})();
var UIController=(函数(){
var DOMstrings={
输入类型:'.添加类型',
inputDescription:'。添加描述',
inputValue:'。添加值',
输入btn:'。添加\u btn',
incomesContainer:“.收入清单”,
费用容器:“.费用清单”
};
返回{
getInput:function(){
返回{
类型:document.querySelector(DOMstrings.inputType).value,
说明:document.querySelector(DOMstrings.inputDescription).value,
值:document.querySelector(DOMstrings.inputValue).value
};
},
getDOMstrings:function(){
返回字符串;
}
};
})();
变量控制器=(函数(budgetCtrl、UICtrl){
var setupEventListeners=函数(){
var DOM=UICtrl.getDOMstrings();
document.querySelector(DOM.inputBtn).addEventListener('click',ctrlAddItem);
document.addEventListener('keypress',函数(事件){
if(event.keyCode==13 | | event.which==13){
ctrlAddItem();
}
});
};
var ctrlAddItem=函数(){
var输入,newItem;
input=UICtrl.getInput();
newItem=budgetCtrl.addItem(input.type,input.description,input.value);
UICtrl.addListItem(newItem,input.type);
};
返回{
init:function(){
setupEventListeners();
}
};
})(预算控制器,UIController);
controller.init();
addListItem函数不起作用。我检查了budgetController函数-没有可疑,那里没有错误,但是当我在控制器函数中调用它时,控制台中出现了错误,我说:
未捕获类型错误:UICtrl.addListItem不是函数
位于HTMLButtonElement.ctrlAddItem(app.js:108)
addListItem
在budgetController
中定义,而不是在UIController
中定义,因此UICtrl.addListItem
不起作用。我真不敢相信我竟然把它放错地方了。祝你今天愉快,善良的陌生人!