javascript记帐薄
时间: 2024-01-01 11:23:37 浏览: 28
以下是一个简单的JavaScript记账薄的示例:
```javascript
// 记账薄构造函数
function Record() {
// 获取缓存的记录内容方法
this.getRecords = function() {
return JSON.parse(localStorage.record);
};
// 计算总收入方法
this.shouru = function() {
var total = 0;
var arr = this.getRecords();
arr.forEach(function(data) {
if (data.type === "+") {
total += data.money / 1;
}
});
return total;
};
// 计算总支出方法
this.zhichu = function() {
var total = 0;
var arr = this.getRecords();
arr.forEach(function(data) {
if (data.type === "-") {
total += data.money / 1;
}
});
return total;
};
// 计算余额方法
this.yue = function() {
return this.shouru() - this.zhichu();
};
}
// 创建记账薄实例
var myRecord = new Record();
// 输出总收入
console.log("总收入: ", myRecord.shouru());
// 输出总支出
console.log("总支出: ", myRecord.zhichu());
// 输出余额
console.log("余额: ", myRecord.yue());
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)