[英]SyntaxError: JSON Parse error: Unexpected identifier “function”
First time posting. 第一次发布。 I have been looking all over for an answer but can seem to find any working solution. 我一直在寻找答案,但似乎可以找到任何可行的解决方案。 I am debugging a shopping cart I built in ES6. 我正在调试我在ES6中构建的购物车。 It seems to work flawlessly on Chrome but on Safari and Firefox I have been getting an error. 它似乎可以在Chrome上完美运行,但是在Safari和Firefox上却出现错误。
Error in Safari: Safari中的错误:
SyntaxError: JSON Parse error: Unexpected identifier "function"
Error in Firefox: Firefox中的错误:
SyntaxError: JSON.parse: unexpected keyword at line 1 column 1 of the JSON data[Learn More]
My code: 我的代码:
export default class productUtil{
constructor (){
}
addToCart (sku, price){
let product={price, quantity:1};
if (sessionStorage.getItem(sku) == undefined){
sessionStorage.setItem(sku, JSON.stringify(product));
}
else {
let oldValue=JSON.parse(sessionStorage.getItem(sku, product));
let newValue = oldValue.quantity + 1;
product.quantity = newValue;
sessionStorage.setItem(sku,JSON.stringify(product));
}
this.cartBuilder(sku, product);
}
cartBuilder(sku, product){
document.getElementById('listItems').innerHTML="";
if (sessionStorage){
for(let key in sessionStorage){
let cartItem = document.createElement("div");
cartItem.setAttribute("id","itemRows");
product = JSON.parse(sessionStorage[key]);
let totalPrice = product.price*product.quantity;
cartItem.innerHTML=(
'<div>'+key+'</div>'+
'<input class="cart_input_size" id="'+key+'" type="number" value="'+product.quantity+'">'+
'<div>'+'$'+product.price+'</div>');
document.getElementById('listItems').appendChild(cartItem);
}
}
}
}
Thank you, appreciate any help! 感谢您的帮助!
You are iterating the prototype also. 您也在迭代原型。 Use Object#hasOwnProperty()
to check if it is prototype or enumerable property 使用Object#hasOwnProperty()
检查它是原型还是可枚举的属性
Try 尝试
for(let key in sessionStorage){
if(sessionStorage.hasOwnProperty(key)){
// process storage item
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.