javascript - 无法读取未定义的 javaScript 的属性“长度”
问题描述
我正在尝试构建预算应用程序,但我得到了他的错误
无法读取未定义的属性“长度”
在 Object.addItem (app.js:36)
在 HTMLButtonElement.ctrlAddItem (app.js:128)
这是我的代码:
// DATA Module
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;
// create new ID
if (data.allItems[type].length > 0) {
ID = data.allItems[type][data.allItems[type].length - 1].id + 1;
} else {
ID = 0;
}
//create new item based on 'inc' or 'exp' type
if(type === 'exp') {
newItem = new Expense(ID, des, val);
} else if (type === 'inc') {
newItem = new Income(ID, des, val);
}
// push it into our data structure
data.allItems[type].push(newItem);
// return the new element
return newItem;
},
testing: function() {
console.log(data)
}
};
})();
var data = {
allItems: {
exp: [1,2,3],
inc: []
},
totals: {
exp: 0,
inc: 0
}
};
// UI module
var UIController = (function() {
var DOMstrings = {
inputType: '.add__type',
inputDescription: '.add__description',
inputValue: '.add__value',
inputBtn: '.add__btn'
};
return {
getInput: function() {
return {
type: document.querySelector(DOMstrings.inputType).value , // will be either inc or exp
description: document.querySelector(DOMstrings.inputDescription).value ,
value: document.querySelector(DOMstrings.inputValue).value
};
},
getDOMstrings: function() {
return DOMstrings;
}
};
})();
// GLOPAL APP CONTROLLER
var controller = (function(budgetCtrl, UICtrl) {
var setupEvenetListeners = 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;
// 1. get the field input data
input = UICtrl.getInput();
// 2. add the item to the budget controller
newItem = budgetCtrl.addItem(input.type, input.description, input.value);
// 3. add the new item to the UI
// 4. calculate the budget
// 5. display the budget on the UI
}
return {
init: function() {
setupEvenetListeners();
}
};
})(budgetController, UIController);
controller.init();
我确定那里有错误但在过去 2 小时内我在这里找不到的行:
if (data.allItems[type].length > 0) {
ID = data.allItems[type][data.allItems[type].length - 1].id + 1;
} else {
ID = 0;
}
这应该从输入字段中获取数据,然后为每个输入数据创建一个唯一 ID,然后将其推送到 data.allItems['exp' or 'inc']
解决方案
推荐阅读
- angular - 如何防止 Angular 应用程序在 Index.html 中加载
- android - Parcelable Map的正确方法?
- joomla - 如何在 Joomla 中为插件添加参数?
- php - 根据列值动态选择列
- mongodb - StreamSets 获取 MongoDB 字段
- git - 如何从另一台计算机更新现有的 github 存储库?
- sql - 将表和数据库服务器传递给存储过程
- spring - 将 Mapstruct 上下文包含到内部映射器中
- php - 它不会显示输出中进度条的百分比,也不会中止上传。我认为 $form 不起作用
- php - 从一台服务器向另一台服务器发送数据,并在接收服务器上查看发送数据