我有一些代码来计算循环中哈希中键/值的和。与其他任何地方相比,它在ios9 Safari上以不同的方式计算和。虽然我可以找到一种方法来修复这个单独的用例,但是我们在我们的大型代码库中使用了这种类型的语法,所以我想了解一下
__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中的一个bug。它既适用于具有'0‘键的散列,也适用于数组。这似乎只是for-in
循环的一个问题。.forEach
,.reduce
等都很好。https://liveweave.com/znUFU2展示了这一点。如果liveweave最初加载速度慢,则刷新页面几次。js小提琴/代码依赖/等等目前不适用于ios9。我已经把这件事报告给了苹果。
发布于 2019-04-14 15:09:30
https://stackoverflow.com/questions/52857259
复制相似问题