本文翻译自:Array state will be cached in iOS 12 Safari. Is it a bug or feature?

Update at 2018.10.31 更新于2018.10.31

This bug has been fixed in iOS 12.1, have a good day~ 这个bug已经在iOS 12.1中得到修复,祝你有个美好的一天〜

I found a problem with Array's value state in the newly released iOS 12 Safari, for example, code like this: 我在新发布的iOS 12 Safari中发现了Array的值状态问题,例如,代码如下:

<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"><title>iOS 12 Safari bugs</title><script type="text/javascript">window.addEventListener("load", function (){let arr = [1, 2, 3, 4, 5];alert(arr.join());document.querySelector("button").addEventListener("click", function (){arr.reverse();});});</script>
</head>
<body><button>Array.reverse()</button><p style="color:red;">test: click button and refresh page, code:</p>
</body>
</html>

After refreshing the page, the array's value is still reversed. 刷新页面后,数组的值仍然相反。 Is this a bug or a feature of new Safari? 这是新Safari的错误还是功能?


Here is a demo page. 这是一个演示页面。 Try to use it with iOS 12 Safari: https://abelyao.github.io/others/ios12-safari-bug.html 尝试在iOS 12 Safari中使用它: https : //abelyao.github.io/others/ios12-safari-bug.html


#1楼

参考:https://stackoom.com/question/3Xp7w/数组状态将在iOS-Safari中缓存-这是一个错误或功能吗


#2楼

I wrote a lib to fix the bug. 我写了一个lib来修复bug。 https://www.npmjs.com/package/array-reverse-polyfill https://www.npmjs.com/package/array-reverse-polyfill

This is the code : 这是代码 :

 (function() { function buggy() { var a = [1, 2]; return String(a) === String(a.reverse()); } if(!buggy()) return; var r = Array.prototype.reverse; Array.prototype.reverse = function reverse() { if (Array.isArray(this)) this.length = this.length; return r.call(this); } })(); 

#3楼

It's definitely a BUG! 这肯定是一个BUG! And it's a very serious bug. 这是一个非常严重的错误。

The bug is due to the optimization of array initializers in which all values are primitive literals. 该错误是由于数组初始值设定项的优化,其中所有值都是原始文字。 For example, given the function: 例如,给定功能:

function buildArray() {return [1, null, 'x'];
}

All returned array references from calls to buildArray() will link to the same memory, and some methods such as toString() will have their results cached. 从对buildArray()调用返回的所有数组都将链接到同一个内存,而某些方法(如toString()将缓存其结果。 Normally, to preserve consistency, any mutable operation on such optimized arrays will copy the data to a separate memory space and link to it; 通常,为了保持一致性,对这种优化数组的任何可变操作都会将数据复制到一个单独的存储空间并链接到它; this pattern is called copy-on-write , or CoW for short. 这种模式称为写时复制 ,或简称为CoW。

The reverse() method mutates the array, so it should trigger a copy-on-write. reverse()方法改变了数组,因此它应该触发copy-on-write。 But it doesn't, because the original implementor (Keith Miller of Apple) missed the reverse() case, even though he had written many testcases. 但它没有,因为原来的实现者(Apple的Keith Miller)错过了reverse()案例,尽管他写了很多测试用例。

This bug was reported to Apple on August 21. The fix landed in the WebKit repository on August 27 and shipped in Safari 12.0.1 and iOS 12.1 on October 30, 2018. 该错误于8月21日向Apple报告 。该修复程序于8月27日登陆WebKit存储库 ,并于2018年10月30日在Safari 12.0.1和iOS 12.1中发布。


#4楼

It seems not to be cached if the number of elements changes. 如果元素的数量发生变化,似乎不会被缓存。
I was able to avoid this like this. 我能够像这样避免这种情况。

 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <title>iOS 12 Safari bugs</title> <script type="text/javascript"> window.addEventListener("load", function () { let arr = [1, 2, 3, 4, 5]; arr.push(''); arr.pop(); alert(arr.join()); document.querySelector("button").addEventListener("click", function () { arr.reverse(); }); }); </script> </head> <body> <button>Array.reverse()</button> <p style="color:red;">test: click button and refresh page, code:</p> </body> </html> 

#5楼

This is a bug in webkit . 这是webkit中的一个错误。 Though this has been solved at their end but not yet shipped with iOS GM release. 虽然这已经在他们的终端解决了但尚未随iOS GM发布一起发布。 One of the solutions to this problem: 解决这个问题的方法之一:

(function() {function getReverseStr() {return [1, 2].reverse();}var n1 = getReverseStr()[0];var n2 = getReverseStr()[0];// check if there is an issueif(n1 != n2) {var origReverseFunction = Array.prototype.reverse;Array.prototype.reverse = function() {var newArr = this.slice();// use original reverse function so that edge cases are taken care oforigReverseFunction.apply(newArr, arguments);var that = this;// copy reversed arraynewArr.forEach(function(value, index) {that[index] = value;});return this;}}
})();

数组状态将在iOS 12 Safari中缓存。这是一个错误或功能吗?相关推荐

  1. php pear mail smtp 不验证,怎么在php中使用pear_smtp实现一个邮件发送功能

    怎么在php中使用pear_smtp实现一个邮件发送功能 发布时间:2020-12-22 16:24:35 来源:亿速云 阅读:76 作者:Leah 怎么在php中使用pear_smtp实现一个邮件发 ...

  2. python中nrows_sheet.nrows有一个错误的值 – python excel文件

    sheet.nrows有一个错误的值 – python excel文件 我有一个很奇怪的问题 我试图从Excel文件中读取一些数据,但属性nrows有一个错误的值. 尽pipe我的文件有很多行,但它只 ...

  3. 给GridView中的buttonField添加一个删除确认功能

    问题: GridView的第一列是ButtonField,字段名是"删除",想一点之后弹出确认框,否则返回.应该如何写? 解决方法: 1.点击GridView的快捷箭头,选'编辑列 ...

  4. 思科(Cisco)IOS 12.3特性分析[ZT]

    cisco的ios 12.3和其子版本不仅包含增加的基本变化和漏洞修复.一起来近距离体验12个最有用的变化,包括网络准入控制(nac),最优边缘路由,动态多点***,ipsec全状态故障恢复等. 可能 ...

  5. ios12上滑关闭程序动画_苹果iOS 12.3正式版发布:上滑清除程序动画重新回归

    新浪科技讯 北京时间5月14日凌晨消息,今天推送了iOS 12.3正式版,随着苹果和专利官司的和解,苹果原先的上滑清除程序动画在iOS 12.3中重新回归,本次更新大小506.2M,苹果除了继续提高系 ...

  6. 新旧 iPhone 比速度,iOS 12 公开测试版发布!

    点击上方"CSDN",选择"置顶公众号" 关键时刻,第一时间送达! 在月初一年一度的 WWDC 上,苹果公司推出了新版操作系统--iOS 12.6 月 20 日 ...

  7. 苹果发布iOS 12.1.4操作系统更新 修复FaceTime安全漏洞

    [TechWeb]2月8日消息,今日凌晨,苹果公司发布了针对 iPhone.iPad 和 iPod touch的iOS 12.1.4操作系统更新,用以修复FaceTime功能的一个安全漏洞. 据悉,F ...

  8. 【算法】剑指 Offer 12. 矩阵中的路径

    文章目录 1.概述 2.我的算法 2.1 棋盘 2.1 开始节点 2.2 点没被访问 2.3 点是否在棋盘内 2.4 下一步 2.5 主方法 2.6 核心方法 2.7 测试类 3.leecode1 1 ...

  9. php ci cors,javascript-CORS请求在Safari中不起作用

    javascript-CORS请求在Safari中不起作用 我正在提出CORS xhr请求. 这在chrome中工作正常,但是当我在野生动物园中运行时,出现"无法加载---- Access- ...

最新文章

  1. PPM / PGM / PBM 图像文件格式
  2. java限频_单个用户及Ip请求频率限制思路(附java实现)
  3. 【源码】java中图片和Base64互相转换源码
  4. python中的魔法参数:*args和**kwargs
  5. Scala range reverse调试
  6. mdp框架_强化学习:MDP(Markov Decision Process)
  7. 春天遇见Apache Hadoop
  8. adb native raact 夜神_React-Native安装及环境搭建(夜神模拟机运行)
  9. SQL语句inner join,left join ,right join连接的不同之处
  10. 口碑销量双爆的数据分析丛书再添新成员!
  11. 【渝粤教育】国家开放大学2019年春季 0755-22T学前儿童艺术教育 参考试题
  12. udhcp源码详解(二)--转
  13. 《计算机操作系统》(第四版)大全总结
  14. c语言智能车跑道检测程序,智能小车的循迹方法与流程
  15. 无线数字信息传送服务器,无线数字远程监控管理及网站实时推广项目方案.doc...
  16. CF14A Letter(模拟+字符串)(C++题解)(大佬勿喷)
  17. cadence 17.2 入门学习2 allegro
  18. 【Unity3D】使用Unity做一个简单的游戏,roll_a_ball,第一步创建游戏。
  19. 【教程】在word中如何替换文字,新手请绕道!
  20. 取消wps右键菜单_wps自动生成论文目录出现错误怎么办

热门文章

  1. Android设备adb调试
  2. ns3安装Windows系统
  3. 求职——1、菊花厂CloudAI 技术LEADER建议学习
  4. 【Beta】设计与计划
  5. 点聚web office解决IE浏览器多个版本兼容问题
  6. 机器学习、监督学习、非监督学习、强化学习传统机器学习、深度学习、迁移学习基本概念
  7. 如何实现IP地址转换
  8. 获取树莓派ip的方法(亲测有效)
  9. Koha的故事,第一个开源图书馆管理系统
  10. VIPLE初学者日记(四)while循环