最近我开始学习javascript,直到遇到无法理解的问题..
说的那部分
arr[lastItem].id
(.id
)的含义是什么?有人可以帮助我了解这一点吗。.谢谢您。
// ---- BUDGET CONTROLLER MODEL
var budgetController = (function() {
// Expenses Constructor
var Expense = function(id, description, value) {
this.id = id;
this.description = description;
this.value = value;
};
// Income Constructor
var Income = function(id, description, value) {
this.id = id;
this.description = description;
this.value = value;
};
// Data Structure
var data = {
allItems: {
expense: [],
income: []
},
totals: {
expense: 0,
income: 0
}
};
return {
addItem: function(type, des, val) {
var newItem;
var ID;
var arr = data.allItems[type];
var len = arr.length;
var lastItem = len - 1; // last item index (position)
// Create & check ID
ID = len > 0 ? arr[lastItem].id + 1 : 0; // <--- what's (.id) mean ???
// Create new item based on expense or income
if (type === 'expense') {
newItem = new Expense(ID, des, val);
} else if (type === 'income') {
newItem = new Income(ID, des, val);
}
// Store the new item
data.allItems[type].push(newItem);
// Return the new item
return newItem;
},
};
})();
完整的代码可以在这里找到 https://codepen.io/safi2266/pen/JQeBQp