ios9 Safari计算错误的总和

时间:2018-10-17 14:25:18

标签: javascript vue.js ios9 ecmascript-5

我有一些代码可以计算循环中哈希值中的键/值之和。与其他任何地方相比,它似乎在ios9 Safari上以不同的方式计算总和。尽管我可以找到一种方法来解决此单个用例,但我们在整个大型代码库中都使用了这种类型的语法,因此我希望对

有一定的了解。
  1. 为什么这会在ios9中发生
  2. 如果有一种全局修复的方法,该方法适用于所有可能带有Vue __ob__对象的对象。

在此处尝试代码:https://liveweave.com/kKo88G。我也将其粘贴在下面:

// Define a hash
var totalItems, sum, type, value
totalItems = {}
totalItems['0'] = 3

// This definition of __ob__ is done dynamically by Vue,
// but I include it here by way of example of what breaks in ios9
totalItems.__ob__ = new Object()
Object.defineProperty(totalItems, '__ob__', {
    enumerable: false,
    writable: true,
   configurable: true
  });

// Loop through the hash
sum = 0
for (type in totalItems) {
  value = totalItems[type];
  sum += value;
}

// sum is 6 in ios9 Safari -- it loops through the '0' key twice 
// sum is 3 in all other browsers and newer ios versions!

更新:

进一步调查后,这似乎是ios9设备上Safari中的错误。它既适用于键为“ 0”的哈希值,也适用于数组。 for-in循环似乎只是一个问题。 .forEach.reduce等可以正常工作。 https://liveweave.com/znUFU2展示了这一点。如果liveweave最初加载缓慢,请刷新页面两次。 js小提琴/ codepen等目前不支持ios9。我已经向苹果报告了这一点。

1 个答案:

答案 0 :(得分:0)

通常将用于...中的不是一个好主意。我相信您的问题与此有关。

您可以对**使用进行...,也可以对进行常规**。

here

中查看有关...的详细信息。