济南PHP培训
济南市达内教育培训学校

17156168575

热门课程

“#smoosh门”引发Web兼容性上的挑战

  • 时间:2018-04-10 14:59
  • 发布:济南PHP培训
  • 来源:互联网

    TC39 提案 Array.prototype.flatten 会导致旧网站在 Firefox Nightly 版中无法正常显示.在回应这一软件缺陷报告时,该新特性的建议者开玩笑称会考虑将"flatten"改名为"smoosh".济南PHP培训的老师说:这在 JavaScript 社区引发了大范围的口诛笔伐.
    这场争论之所以愈演愈烈,不仅在于建议更改名称这个玩笑,更是因为在不破坏 Web 兼容性的条件下如何推进 JavaScript 语言的问题上存在着挑战.
    作为一项提案,Array.prototype.flatten 意在解决一个通常由 JavaScript 软件库和架构解决的问题,即如何将嵌套数组展平为单一数组.例如:
    该 API 的更改,本身是对 JavaScript 中的一个常见模式的改进.不幸的是,一些 JavaScript 软件库和框架,例如 Prototype 和 MooTools 等,其早期版本中为添加对此特性的支持,采用了一种扩展内建语言特性原生原型的模式.很多软件库最终认识到,这并非是一种好做法,因而决定另辟蹊径去实现语言上的改进.
    问题由一家名为"wetteronline"的德国气象网站爆出.最终,问题的讨论转向对为什么 Space Jam 网站自 1996 年至今一直正常工作的关注,尽管 Space Jam 网站本身并未受到"#smoosh 门"的影响.
    MooTools 中定义的 Array.prototype.flatten,是一种不同于 TC39 建议标准的非标准版本.MooTools 覆盖了浏览器的原生实现,这一做法本身并不存在问题.但是,MooTools 将其定制的数组方法克隆到 Elements.prototype API.JavaScript 的"for-in"只支持遍历可枚举属性,不包括原生方法.如果工程师覆写了一个不可枚举属性,该属性依然是不可枚举的.这样,原生版本的 Array.prototype.flatten 并未拷贝到 MooTools 的 Elements API,破坏了 MooTools 的 Elements.prototype.flatten.
    当前 Web 网站上的挑战已转变为,一旦旧软件库中已实现了一些限制引入非破坏性更改能力的特性,人们难以确定如何扩展 Web.一些从长期缺乏维护的网站虽然依然工作,但是一旦添加了不兼容的改进,就会不工作.这时,让编写网站的人去实现升级可能并不现实,或许在网站不再需要活跃维护的情况下才有可能.
    为避免将来出现此类问题,不鼓励工程师去扩展或替换原生的对象或特性,也不要扩展全局命名空间.就当前的 ES2015+ 而言,Symbols 等特性提供了更好的机制,可在需要时扩展原生对象,而不会更改内建原生对象的行为.
    为终结该提案以添加到 ECMAScript 的将来版本中,所考虑的一些替代方案包括:对 flatten 更名(当然不是 smoosh)、将 flatten 作为 Array.prototype 上的值存取器(accessor pair),以及为 flatten 新建一个固有对象.
    "#smoosh 门"并非 JavaScript 生态系统中首次爆发的大规模辩论.以前曾发生过涉及"left-pad"和"尾随分号"(trailing semicolons)等问题的辩论.
    #smooshgate and the Challenges of Web Compatibility
    The broader JavaScript community responded vocally when Michael Ficarra, the author of the Array.prototype.flatten TC39 proposal, jokingly suggested renaming flatten as smoosh in response to a bug report that the new feature breaks old websites in nightly versions of Firefox.
    The debate occurred not merely because of the alternative name that was proposed, but because of the challenges in advancing the language without breaking web compatibility.
    Array.prototype.flatten is a proposal to address a problem often solved by JavaScript libraries and frameworks, namely to take a nested array and flatten it into a single array. For example:

    var arr1 = [1, 2, [3, 4]];

济南PHP培训

    arr1.flatten();
    // [1, 2, 3, 4]
    This API change itself is an improvement to a common pattern within JavaScript. Unfortunately, early versions of some JavaScript libraries and frameworks such as Prototype and MooTools embraced a pattern of extending the native prototype of built-in language features to add support. Most libraries eventually realized that this was not a great idea, and used other approaches to provide language enhancements.
    The issue was reported from wetteronline, a German weather website, though eventually, the discussion led to notes about how the Space Jam website from 1996 still works today, though the Space Jam website is not itself impacted by #smooshgate.
    MooTools defines a non-standard version of Array.prototype.flatten which differs from the proposed standard. However, that alone is not the problem as MooTools overrides the native browser implementation. MooTools clones its custom array methods to its Elements.prototype API. JavaScript's for-in only iterates over enumerable properties, excluding native methods. If an engineer overwrites a non-enumerable property, it remains non-enumerable. Thus the native version of Array.prototype.flatten is not copied to MooTools Elements API, breaking MooTools' Elements.prototype.flatten.
    The challenge with a long-lived system like the World Wide Web is that it is difficult to determine how to extend the programming languages used when old libraries have implemented features that limit the ability to introduce non-breaking changes. Many websites still work, but are long since forgotten and would be lost if incompatible changes get added to the web. Therefore, asking a website author to upgrade may not be feasible or possible when the site is no longer actively maintained.
    To avoid issues like this is in the future, engineers are encouraged to not extend or replace native objects or features, and to not extend the global namespace. Today with ES2015+, features such as Symbols would be a better mechanism for extending native objects as needed, without changing the behavior of built-in native objects.
    To finalize this proposal for inclusion in a future version of ECMAScript, alternatives under consideration include an alternative name for flatten (not smoosh), making flatten an accessor pair on Array.prototype, or creating a new intrinsic object for flatten.

    #smooshgate is not the first time the JavaScript ecosystem has had a widely debated discussion. Previous examples include left-pad and trailing semicolons.

更多济南PHP培训相关咨询,请扫描下方二维码

济南Android培训

上一篇:PHP大型网站的架构实例分析
下一篇:PHP翻页类 调用实例

马上预约七天免费体验课

姓名:

电话:

什么样的语言编程更有利于网站优化?

php应用:php快速入门

PHP并没有死

什么是PHP框架,为什么要用PHP框架

选择城市和中心
贵州省

广西省

海南省

达内教育

有位老师想和您聊一聊