JavaScript localStorage:如何使用JSON.stringify从保存的数组对象解析方法
我有以下JavaScript代码:JavaScript localStorage:如何使用JSON.stringify从保存的数组对象解析方法,javascript,json,local-storage,session-storage,Javascript,Json,Local Storage,Session Storage,我有以下JavaScript代码: function Product(){ this.ProductName=""; this.Quantity=0; this.Price=0; this.Category=""; this.Total=function() { return this.Quantity * this.Price; } this.Discount=function() { return this.Total() * 0.25; } } var
function Product(){
this.ProductName="";
this.Quantity=0;
this.Price=0;
this.Category="";
this.Total=function() {
return this.Quantity * this.Price;
}
this.Discount=function() {
return this.Total() * 0.25;
}
}
var Product = new Product();
Product.ProductName="Tipkovnica";
Product.Quantity=100;
Product.Price=150.5;
Product.Category="IT";
if(localStorage.Products){
Products = JSON.parse(localStorage.getItem("Products"));
}
else
{
var Products = [];
}
Products.push(Product);
localStorage.setItem('Products', JSON.stringify(Products));
function RetrieveObjects(Products){
for(var a=0;a<Products.length; a++){
document.write("<br>Product Name: "+Products[a].ProductName);
document.write("<br>Quantity: "+Products[a].Quantity);
document.write("<br>Price: "+Products[a].Price);
document.write("<br>Category: "+Products[a].Category);
document.write("<br>Total: "+Products[a].Total());
document.write("<br>Discount: "+Products[a].Discount());
}
}
函数产品(){
此.ProductName=“”;
这个。数量=0;
这个价格=0;
本类别=”;
this.Total=函数(){
返回此数量*此价格;
}
this.Discount=function(){
返回此.Total()*0.25;
}
}
var Product=新产品();
Product.ProductName=“Tipkovnica”;
产品数量=100;
产品价格=150.5;
Product.Category=“IT”;
if(localStorage.Products){
Products=JSON.parse(localStorage.getItem(“Products”);
}
其他的
{
var乘积=[];
}
产品。推(产品);
localStorage.setItem('Products',JSON.stringify(Products));
函数检索对象(产品){
对于(var a=0;a,因为如果将对象字符串化,则所有方法都将被删除。请看以下内容:
不相关提示:对所有方法、属性和变量使用camelCase而不是PascalCase约定。PascalCase应在类名、接口等中使用。您的函数正在使用此
,但这是全局此
,它是窗口
对象
但是,还有一个问题,您不能(或者不应该)在JSON中存储函数,因为没有函数数据类型。您应该计算值并将结果存储在JSON中
var Product {
productName: null,
quantity: 0,
price: 0,
category: null,
finalTotal: 0,
discountedTotal: 0,
total: function() {
return this.quantity * this.price;
},
discount: function() {
return this.total() * 0.25;
}
}
var newProduct = Object.create(Product);
newProduct.productName = "Tipkovnica";
newProduct.quantity = 100;
newProduct.price = 150.5;
newProduct.category = "IT";
newProduct.finalTotal = newProduct.total();
newProduct.discountedTotal = newProduct.discount();
if (localStorage.Products) {
Products = JSON.parse(localStorage.getItem("Products"));
Products.push(newProduct);
} else {
Products = [newProduct];
}
localStorage.setItem('Products', JSON.stringify(Products));
function RetrieveObjects(Products) {
for (var a = 0; a < Products.length; a++) {
document.write("<br>Product Name: " + Products[a].productName);
document.write("<br>Quantity: " + Products[a].quantity);
document.write("<br>Price: " + Products[a].price);
document.write("<br>Category: " + Products[a].category);
document.write("<br>Total: " + Products[a].finalTotal);
document.write("<br>Discount: " + Products[a].discountedTotal);
}
}
var乘积{
productName:null,
数量:0,
价格:0,,
类别:空,
最终总数:0,
折扣总额:0,
总计:函数(){
返回此.quantity*此.price;
},
折扣:函数(){
返回此.total()*0.25;
}
}
var newProduct=Object.create(产品);
newProduct.productName=“Tipkovnica”;
newProduct.quantity=100;
newProduct.price=150.5;
newProduct.category=“IT”;
newProduct.finalTotal=newProduct.total();
newProduct.discountedTotal=newProduct.discount();
if(localStorage.Products){
Products=JSON.parse(localStorage.getItem(“Products”);
产品。推送(新产品);
}否则{
产品=[新产品];
}
localStorage.setItem('Products',JSON.stringify(Products));
函数检索对象(产品){
对于(var a=0;a产品名称:“+Products[a].productName”);
单据.填写(
数量:“+产品[a].数量);
文件。写(“
价格:”+产品[a]。价格);
文件。写入(“
类别:”+产品[a]。类别);
填写(“
总计:”+产品[a].finalTotal);
单据.写(“
折扣:”+产品[a].折扣总额);
}
}