简体   繁体   English

预期为“;” IE中的异常(JavaScript,jQuery)

[英]Expected ';' exception in I.E (JavaScript, jQuery)

I'm testing my website for IE compatibility and right away I run into plethora of problems: 我正在测试我的网站的IE兼容性,并且立即遇到很多问题:

DOM7011: The code on this page disabled back and forward caching. For more information, see: http://go.microsoft.com/fwlink/?LinkID=291337
webshop.html
HTML1300: Navigation occurred.
webshop.html
SCRIPT1004: Expected ';'
main.js (16,18)

At this point I'm mostly concerned with the ; 在这一点上,我最关心的是; not being expected because every time i check my code it's THERE. 没想到,因为每次我检查我的代码都在那儿。

在此处输入图片说明

IE doesn't support for..of , so you can either transpile your code (eg with Babel ) or use a good old forEach : IE 不支持for..of ,因此您可以(例如使用Babel )转换代码或使用旧的forEach

this.cartShop.forEach(function(item) {
    if (...) { ... }
}, this);

Update : I'm seeing now that inside the if block there's a return statement to break out of the function early, and that is why the code I suggested behaves differently. 更新 :我现在看到,在if块中有一个return语句,可以尽早退出该函数,这就是为什么我建议的代码表现不同的原因。 I don't recommend this kind of approach, as it leads to unexpected behaviors and code duplication. 我不推荐这种方法,因为它会导致意外的行为和代码重复。 As a norm, get out of a loop using break . 通常,使用break退出循环。

I see you first are checking if the item is in the cart: you can do it using find : 我看到您首先要检查物品是否在购物车中:您可以使用find

var item = this.cartShop.find(function(item) {
    return item.name === name && ...
});

if (item) {
    item.quantity += quantity;
} else {
    item = new this.Item(...);
    this.cartShop.push(item);
}

this.saveLocalCart();

Unfortunately, find is again not available in IE, but contrary to for..of you can simply provide a polyfill . 不幸的是, find又是不是在IE可用,但违背for..of你可以简单地提供一个填充工具 Alternatively, you can always use your old code with a classic for loop: 另外,您始终可以将旧代码与经典的for循环一起使用:

for (var i = 0; i < this.cartShop.length; i++) {
    var item = this.cartShop[i];
    if (...) { ... }
}

More verbose, less readable, but still effective. 更详细,更易读,但仍然有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM